LocalDateTime date3 = LocalDateTime.now();
LocalDateTime date4 = LocalDateTime.of(2022, 10, 8, 10, 30, 10);
Duration duration = Duration.between(date3, date4);
System.out.println(date3 + " 与 " + date4 + " 间隔 " + "\n"
+ " 天 :" + duration.toDays() + "\n"
+ " 时 :" + duration.toHours() + "\n"
+ " 分 :" + duration.toMinutes() + "\n"
+ " 秒 :" + duration.getSeconds() + "\n"
+ " 毫秒 :" + duration.toMillis() + "\n"
+ " 纳秒 :" + duration.toNanos() + "\n"
);
参考文章

本文演示了如何使用Java中的LocalDateTime与Duration类来计算两个日期时间之间的差异。通过实例代码展示了获取不同单位(天、小时、分钟、秒等)的时间间隔。

953

被折叠的 条评论
为什么被折叠?



