1.首先引入jar包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.7</version>
</dependency>
2.计算差值
public static DateFormat getDateTimeFormat(){
DateFormat dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dtf;
}
//获取两个时间之前的差值(小时)
public static String getSjScz(String rq1,String rq2)throws Exception{
DateFormat simpleFormat = getDateTimeFormat();
Date timeDate1 = simpleFormat.parse(rq1);
Date timeDate2 = simpleFormat.parse(rq2);
long hour=DateUtil.between(timeDate1, timeDate2, DateUnit.HOUR);
return String.valueOf(hour);
}
//获取两个时间之前的差值(小时带小数)
public static String getSjSch(String rq1,String rq2)throws Exception{
DateFormat simpleFormat = getDateTimeFormat();
Date timeDate1 = simpleFormat.parse(rq1);
Date timeDate2 = simpleFormat.parse(rq2);
long time1 = timeDate1.getTime();
long time2 = timeDate2.getTime();
double minutes = ((time1 - time2)*1.0 / (1000 * 60*60));
return String.valueOf(minutes);
}
//获取两个时间之前的差值(分钟)
public static String getSjScm(String rq1,String rq2)throws Exception{
DateFormat simpleFormat = getDateTimeFormat();
Date timeDate1 = simpleFormat.parse(rq1);
Date timeDate2 = simpleFormat.parse(rq2);
long minutes=DateUtil.between(timeDate1, timeDate2, DateUnit.MINUTE);
return String.valueOf(minutes);
}