看了java8时间的计算时间差:
http://www.php.cn/java-article-407521.html
大概有三种 Period:
LocalDate today = LocalDate.now();
LocalDate birthDate = LocalDate.of(1993, Month.OCTOBER, 19);
System.out.println(
"BirthDate : "
+ birthDate);
Period p = Period.between(birthDate, today);
System.out.printf(
"年龄 : %d 年 %d 月 %d 日"
, p.getYears(), p.getMonths(), p.getDays());
Duration:
Duration duration= Duration.between( date1.toInstant(),date2.toInstant());
long t=duration.toDays();
你也可以转换整个时间到其它单位如纳秒、分钟、小时、天:
long seconds = getSeconds()
int nanos = getNano()
toNanos()
toMillis()
toMinutes()
toHours()
toDays()
一些加减的方法:https://blog.csdn.net/tjgykhulj/article/details/69053873
ChronoUnit:
LocalDate startDate = LocalDate.of(1993, Month.OCTOBER, 19);
System.out.println(
"开始时间 : "
+ startDate);
LocalDate endDate = LocalDate.of(2017, Month.JUNE, 16);
System.out.println(
"结束时间 : "
+ endDate);
long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
System.out.println(
"两天之间的差在天数 : "
+ daysDiff);