Duration.ofDays 生成 UnsupportedTemporalTypeException

Duration.ofDays generates UnsupportedTemporalTypeException

我正在努力学习新的 Date & Time API。除了最后一行,我的代码正常工作:

LocalDate current=LocalDate.now();
System.out.println(current);

LocalDate personaldate=LocalDate.of(2011,Month.AUGUST, 15);
System.out.println(personaldate);

LocalDate afterten=current.plus(Period.ofDays(10));
System.out.println(afterten);

// error occurs here        
System.out.println(afterten.plus(Duration.ofDays(3)));

当我尝试添加以天为单位的持续时间时,它会生成错误。谁能帮我理解为什么?

错误:

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds                                                                                             
        at java.time.LocalDate.plus(LocalDate.java:1241)                                                                                                                                              
        at java.time.LocalDate.plus(LocalDate.java:137)                                                                                                                                               
        at java.time.Duration.addTo(Duration.java:1070)                                                                                                                                               
        at java.time.LocalDate.plus(LocalDate.java:1143)                                                                                                                                              
        at TestClass.main(TestClass.java:15)    

持续时间使用基于时间的值(秒、纳秒)测量时间量。 Period 使用基于日期的值(年、月、日)。 这是 link

https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

与 JodaTime 相同

虽然接受的答案是完全正确的,但当我遇到这个问题时,我正在寻找一个简单的解决方案来解决我的问题。

我发现使用 Period 不允许我计算两个 LocalDate 对象之间的天数。 (告诉我这两者之间有多少年,几个月和几天,是的,但不仅仅是那几天。)

但是,要获得我想要的结果,只需将 LocalDate 方法 "atStartOfDay" 添加到我的每个对象即可。

所以我的错误代码:

long daysUntilExpiry = Duration.between(LocalDate.now(), training.getExpiryDate()).toDays();

简单调整为:

long daysUntilExpiry = Duration.between(LocalDate.now().atStartOfDay(), training.getExpiryDate().atStartOfDay()).toDays();

这样做会使对象变成可以与 Duration 一起使用的 LocalDateTime 对象。因为两个对象都将一天的开始作为 "time" 部分,所以没有区别。

希望这对其他人有帮助。

如前所述,持续时间始终以秒为单位,而期间则以日为概念。 当代码尝试在 LocalDate 上添加秒数时抛出异常 - 这也是基于天的。

像这样更改您的代码会显示出不同之处:在几天内进入即时状态时使用 LocalDateTime:

LocalDateTime current = LocalDateTime.now();
System.out.println(current);

LocalDateTime afterten = current.plus(Period.ofDays(10));
System.out.println(afterten);

// error occurred here - but with LocalDateTime is resolved!
System.out.println(afterten.plus(Duration.ofDays(3)));
//(year,month,day)

LocalDate beginDate = LocalDate.of(1899,12,31);    
LocalDate today = LocalDate.now();    
ChronoUnit.DAYS.between(beginDate, today)

尝试 运行 遵循单元测试中的代码,并亲眼看看您的问题的可接受答案应该是 ChronoUnit.DAYS.between() 如前所述拉维

LocalDate date1 = LocalDate.of(2020,6,2);
LocalDate date2 = LocalDate.of(2020,7,4);
System.out.printf("ChronoUnit.DAYS = %d%n", ChronoUnit.DAYS.between(date1, date2));
System.out.printf("Period.between = %d%n",Period.between(date1, date2).getDays());

因为输出如下所示:

ChronoUnit.DAYS = 32
Period.between = 2

期间将错误地 return 只有天数部分的差异(忽略更高阶的差异,如月和年)。

System.out.printf("Duration.between = %d%n",Duration.between(date1, date2).getSeconds());

这将引发异常,因为 LocalDate 没有为秒计算提供足够的信息(未定义的小时、分钟和秒)。 因此,您必须将其转换为 LocalDateTime,例如通过调用 date1.atStartOfDay().

System.out.printf("Duration.between = %d%n",Duration.between(date1.atStartOfDay(), date2.atStartOfDay()).get(ChronoUnit.DAYS));

此调用将简单地抛出 java.time.temporal.UnsupportedTemporalTypeException:不受支持的单位:天,因为 Duration class 上的 get 方法是如何在 Java 中实现的:

@Override
public long get(TemporalUnit unit) {
    if (unit == SECONDS) {
        return seconds;
    } else if (unit == NANOS) {
        return nanos;
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}