您可以使用方法 之间 的
ChronoUnit
。
此方法将那些时间转换为相同的区域(第一个参数的区域),然后,调用 直到 在 Temporal 接口中声明的方法 为止 :
static long zonedDateTimeDifference(ZonedDateTime d1, ZonedDateTime d2, ChronoUnit unit){ return unit.between(d1, d2);}由于 ZonedDateTime 和 LocalDateTime都 实现了 Temporal
接口,因此您还可以为这些日期时间类型编写通用方法:
static long dateTimeDifference(Temporal d1, Temporal d2, ChronoUnit unit){ return unit.between(d1, d2);}但是请记住,为混合 LocalDateTime 和 ZonedDateTime 调用此方法会导致 DateTimeException
。
希望能帮助到你。



