1.直接算差
public static long getDiff(String time1,String time2) throws ParseException {
//比较接收请求的时间和系统记录时间差
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sd.parse(time1);
Date date2 = sd.parse(time2);
//得到2个日期的总毫秒数
long s1 = date1.getTime();//时间的毫秒
long s2 = date2.getTime();
long diff = s2-s1;
return diff;
}
2.方法2:直接得到时间的毫秒数
/**
* 将传入的yyyy-MM-dd HH:mm:ss字符类型的时间转换成毫秒数
*
* @param time
* @return
* @throws ParseException
*/
public static long getMillisecond(String time) {
logger.info("传入参数为:" + time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long millisecond = 0l;
try {
Date date = sdf.parse(time);
millisecond = date.getTime();
} catch (ParseException e) {
logger.info("时间格式转换异常");
e.printStackTrace();
}
return millisecond;
}
3.其他各种与时间相关:
/**
* 获取时间戳
*
* @return yyyyMMddHHmmss
*/
public static String getTime() {
return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
}
/**
* 获取格式为yyyy-MM-dd的系统时间
*
* @return yyyy-MM-dd
*/
public static String getTimes() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
/**
* 获取格式为yyyy-MM-dd HH:mm:ss的系统时间
*
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getSystemTime() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
/**
* 获取格式为yyyy-MM-dd HH:mm系统时间
*
* @return yyyy-MM-dd HH:mm
*/
public static String getSystemTimes() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
}
/**
* 获取yyyy年MM月dd日 HH:mm格式的系统时间
*
* @return
*/
public static String getChineseFormatTime() {
return new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(new Date());
}
/**
* 过去当前的年月
*/
public static String getCurrentYearAndMonth() {
return new SimpleDateFormat("yyyyMM").format(new Date());
}
/**
* 把传入的日期类型的数据转换成字符串类型
*
* @param date
* @return yyyy-MM-dd HH:mm:ss
*/
public static String dateToString(Date date) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
/**
* 将传入的yyyy-MM-dd HH:mm:ss字符类型的时间转换成毫秒数
*
* @param time
* @return
* @throws ParseException
*/
public static long getMillisecond(String time) {
logger.info("传入参数为:" + time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long millisecond = 0l;
try {
Date date = sdf.parse(time);
millisecond = date.getTime();
} catch (ParseException e) {
logger.info("时间格式转换异常");
e.printStackTrace();
}
return millisecond;
}
/**
* 将传入的yyyy-MM-dd字符类型的时间转换成毫秒数
*
* @param time
* @return
* @throws ParseException
*/
public static long getDayMillisecond(String time) {
logger.info("传入参数为:" + time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
long millisecond = 0l;
try {
Date date = sdf.parse(time);
millisecond = date.getTime();
} catch (ParseException e) {
logger.info("时间格式转换异常");
e.printStackTrace();
}
return millisecond;
}
/**
* 获取当前日期是星期几
*
* @return
*/
public static String getDay() {
return new SimpleDateFormat("EEEE").format(new Date());
}
/**
* 获得格式为yyyy.MM.dd的当前日期
*
* @return
*/
public static String getDate() {
return new SimpleDateFormat("yyyy.MM.dd").format(new Date());
}