【发布时间】:2017-11-15 04:52:47
【问题描述】:
在 Joda 中,我们可以计算 2 个日期时间之间的年份
Years.between(dateTime1, dateTime2);
是否有任何简单的方法可以使用java.time API 来查找 2 个瞬间之间的年份,而不需要太多逻辑?
ChronoUnit.YEARS.between(instant1, instant2)
失败:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
at java.time.Instant.until(Instant.java:1157)
at java.time.temporal.ChronoUnit.between(ChronoUnit.java:272)
...
【问题讨论】:
-
Instance1是什么类型? -
刚刚更新。它是 java.time.Instant。
标签: java datetime java-8 date-difference java.time.instant