Java8 Time

计算差异

Period

LocalDate today = LocalDate.now();
LocalDate birthDate = LocalDate.of(1993, Month.OCTOBER, 19);
Period p = Period.between(birthDate, today);
System.out.printf("%d 年 %d 月 %d 日", p.getYears(), p.getMonths(), p.getDays());

Duration

Instant inst1 = Instant.now();
Instant inst2 = inst1.plus(Duration.ofSeconds(10));
System.out.println("Difference in milliseconds : " + Duration.between(inst1, inst2).toMillis());
System.out.println("Difference in seconds : " + Duration.between(inst1, inst2).getSeconds());

ChronoUnit

LocalDate startDate = LocalDate.of(1993, Month.OCTOBER, 19);
LocalDate endDate = LocalDate.of(2017, Month.JUNE, 16);
long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
System.out.println("两天之间的差在天数   : " + daysDiff);

你可能感兴趣的:(Java8 Time)