1、传统的SimpleDateFormat类
/**
* 用SimpleDateFormat计算时间差
* @throws ParseException
*/
public static void calculateTimeDifferenceBySimpleDateFormat() throws ParseException {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
/*天数差*/
Date fromDate1 = simpleFormat.parse("2018-03-01 12:00");
Date toDate1 = simpleFormat.parse("2018-03-12 12:00");
long from1 = fromDate1.getTime();
long to1 = toDate1.getTime();
int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));
System.out.println("两个时间之间的天数差为:" + days);
/*小时差*/
Date fromDate2 = simpleFormat.parse("2018-03-01 12:00");
Date toDate2 = simpleFormat.parse("2018-03-12 12:00");
long from2 = fromDate2.getTime();
long to2 = toDate2.getTime();
int hours = (int) ((to2 - from2) / (1000 * 60 * 60));
System.out.println("两个时间之间的小时差为:" + hours);
/*分钟差*/
Date fromDate3 = simpleFormat.parse("2018-03-01 12:00");
Date toDate3 = simpleFormat.parse("2018-03-12 12:00");
long from3 = fromDate3.getTime();
long to3 = toDate3.getTime();
int minutes = (int) ((to3 - from3) / (1000 * 60));
System.out.println("两个时间之间的分钟差为:" + minutes);
}
2、java 8中的周期类Period
通过调用Period类的静态方法between,传入两个待比较的LocalDate对象today与oldDate,得到的Period的对象p中就包含了today与oldDate两个日期相差的年、月、日信息,可以通过p.getYears()等方法取出
/**
* 使用java 8的Period的对象计算两个LocalDate对象的时间差,严格按照年、月、日计算,如:2018-03-12 与 2014-05-23 相差 3 年 9 个月 17 天
* @param year
* @param month
* @param dayOfMonth
*/
public static void calculateTimeDifferenceByPeriod(int year, Month month, int dayOfMonth) {
LocalDate today = LocalDate.now();
System.out.println("Today:" + today);
LocalDate oldDate = LocalDate.of(year, month, dayOfMonth);
System.out.println("OldDate:" + oldDate);
Period p = Period.between(oldDate, today);
System.out.printf("目标日期距离今天的时间差:%d 年 %d 个月 %d 天\n", p.getYears(), p.getMonths(), p.getDays());
}
Today:2018-03-13
OldDate:2014-05-23
目标日期距离今天的时间差:3 年 9 个月 18 天
3、java 8中的Duration类
Duration与Period相对应,Period用于处理日期,而Duration计算时间差还可以处理具体的时间,也是通过调用其静态的between方法,该方法的签名是between(Temporal startInclusive, Temporal endExclusive),因此可以传入两个Instant的实例(Instant实现了Temporal接口),并可以以毫秒(toMillis)、秒(getSeconds)等多种形式表示得到的时间差
public static void calculateTimeDifferenceByDuration() {
Instant inst1 = Instant.now(); //当前的时间
System.out.println("Inst1:" + inst1);
Instant inst2 = inst1.plus(Duration.ofSeconds(10)); //当前时间+10秒后的时间
System.out.println("Inst2:" + inst2);
Instant inst3 = inst1.plus(Duration.ofDays(125)); //当前时间+125天后的时间
System.out.println("inst3:" + inst3);
System.out.println("以毫秒计的时间差:" + Duration.between(inst1, inst2).toMillis());
System.out.println("以秒计的时间差:" + Duration.between(inst1, inst3).getSeconds());
}
Inst1:2018-03-13T09:06:00.691Z
Inst2:2018-03-13T09:06:10.691Z
inst3:2018-07-16T09:06:00.691Z
以毫秒计的时间差:10000
以秒计的时间差:10800000
4、java 8中的ChronoUnit类
ChronoUnit的between方法签名为,between(Temporal temporal1Inclusive, Temporal temporal2Exclusive),需要注意的是,如果要以不同的单位展示时间差,between入参中的时间对象必须包含有对应的时间信息,否则会抛出java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit XXX的异常
/**
* 使用java 8的ChronoUnit,ChronoUnit可以以多种单位(基本涵盖了所有的,看源码发现竟然还有“FOREVER”这种单位。。)表示两个时间的时间差
*/
public static void calculateTimeDifferenceByChronoUnit() {
LocalDate startDate = LocalDate.of(2003, Month.MAY, 9);
System.out.println("开始时间:" + startDate);
LocalDate endDate = LocalDate.of(2015, Month.JANUARY, 26);
System.out.println("结束时间:" + endDate);
long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
System.out.println("两个时间之间的天数差为:" + daysDiff);
// long hoursDiff = ChronoUnit.HOURS.between(startDate, endDate); //这句会抛出异常,因为LocalDate表示的时间中不包含时分秒等信息
}
开始时间:2003-05-09
结束时间:2015-01-26
两个时间之间的天数差为:4280