【发布时间】:2020-02-10 09:35:36
【问题描述】:
我使用 java Duration between 方法来计算 LocalTime 和 LocalDateTime 之间的持续时间,当我将 localTime 作为方法的第一个参数时它是有效的,但是当我颠倒顺序时它给出了异常 DateTimeException:
这项工作很好:
LocalTime t1 = LocalTime.now();
LocalDateTime t2 = LocalDateTime.now();
System.out.println(Duration.between(t1, t2));
当我反转 agrs 时,它给了我一个例外:
LocalTime t1 = LocalTime.now();
LocalDateTime t2 = LocalDateTime.now();
System.out.println(Duration.between(t2, t1));
Exception in thread "main" java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: 16:32:18.553 of type java.time.LocalTime
有人可以解释为什么我得到这个异常感谢任何帮助:=)
【问题讨论】:
-
如果我问你从 9 月 19 日到现在还有多少时间,答案应该是什么? 2019 年 12:00 和 09:12:34?我的答案是:哪一天 09:12:34?这也是 Duration.between 提供的答案。