我正在尝试学习新的日期和时间 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.between(date1, date2).days
可以实现这个功能。 - DemigodPeriod.getDays
返回完整的天数。我错了。谢谢! - Demigod