看好了,我只表演一次
public class DateTest03 {
public static void main(String[] args) throws ParseException {
String dealTime = "2023-03-07 18:22:10";
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = time.parse(dealTime);
String dealTime1 = "2023-03-10 15:22:10";
SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse1 = time1.parse(dealTime1);
int timeDifferenceOfHour = getTimeDifferenceOfHour(parse, parse1);
System.out.println(timeDifferenceOfHour);
}
public static int getTimeDifferenceOfHour(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少小时
long hour = diff % nd / nh;
return Integer.parseInt(String.valueOf(hour));
}
}
更简单的来了
public static void main(String[] args) {
long result = cn.hutool.core.date.DateUtil.between(startTime(), new Date(), DateUnit.HOUR);
}