时间差
Date date1 = new Date();
Date date2 = new Date();
//转换时间格式
long time1 = date1.getTime(); //这里是举例子,实际运用需要用自己的时间替换
long time2 = date2.getTime();
Instant pastInstant1 = Instant.ofEpochMilli(time1);
Instant pastInstant2 = Instant.ofEpochMilli(time2);
Duration duration = Duration.between(pastInstant1 , pastInstant2 );//可能会计算出来负数,那就是pastInstant1 在pastInstant2之后
//获取当前时间
//Instant current = Instant.now();
System.out.println("时间差(天):" + duration.toDays());
System.out.println("时间差(小时):" + duration.toHours());
System.out.println("时间差(分钟):" + duration.toMinutes());
System.out.println("时间差(秒):" + duration.toMillis());
天、小时、分钟、秒都可以输出。