java Duration.ofDays 生成 UnsupportedTemporalTypeException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34440874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Duration.ofDays generates UnsupportedTemporalTypeException
提问by user2779311
I am trying to learn the new Date & Time API. My code is working except for the last line:
我正在尝试学习新的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)));
When I try and add a Duration in days, it generates an error. Can anyone help me understand why?
当我尝试以天为单位添加 Duration 时,它会生成错误。谁能帮我理解为什么?
Error:
错误:
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)
回答by wombling - Chris Paine
Whilst the accepted answer is completely correct, when I arrived at this question, I was looking for a simple solution to my problem.
虽然接受的答案是完全正确的,但当我遇到这个问题时,我正在寻找一个简单的解决方案来解决我的问题。
I found using Period would not allow me to count the number of days between my two LocalDate objects. (Tell me how many years, months and days between the two, yes, but not just then number of days.)
我发现使用 Period 不允许我计算两个 LocalDate 对象之间的天数。(告诉我两者之间有多少年、月和日,是的,但不仅仅是天数。)
However, to get the result I was after was as simple as adding the LocalDate method "atStartOfDay" to each of my objects.
但是,要获得我想要的结果就像将 LocalDate 方法“atStartOfDay”添加到我的每个对象一样简单。
So my erronious code:
所以我的错误代码:
long daysUntilExpiry = Duration.between(LocalDate.now(), training.getExpiryDate()).toDays();
was simply adjusted to:
简单地调整为:
long daysUntilExpiry = Duration.between(LocalDate.now().atStartOfDay(), training.getExpiryDate().atStartOfDay()).toDays();
Doing this make the objects into LocalDateTime objects which can be used with Duration. Because both object have start of day as the "time" part, there is no difference.
这样做会使对象成为可与 Duration 一起使用的 LocalDateTime 对象。因为两个对象都将一天的开始作为“时间”部分,所以没有区别。
Hope this helps someone else.
希望这对其他人有帮助。
回答by Azat Nugusbayev
A Duration measures an amount of time using time-based values (seconds, nanoseconds). A Period uses date-based values (years, months, days). here is the link
持续时间使用基于时间的值(秒、纳秒)来测量时间量。Period 使用基于日期的值(年、月、日)。链接在这里
https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
the same as in JodaTime
与 JodaTime 相同
回答by avogt
As hinted before, Duration is always seconds-based whereas Period honours the day as concept. The code throws an exception when it tries to add seconds on a LocalDate - which is also day-based.
如前所述,Duration 始终以秒为单位,而 Period 将日期视为概念。当它尝试在 LocalDate 上添加秒时,代码会抛出异常 - 这也是基于天的。
Changing your code like this shows the difference: use LocalDateTime when getting down to instants within days:
像这样更改代码显示了不同之处:在几天内进入瞬间时使用 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)));
回答by Ravi Jaganathan
//(year,month,day)
LocalDate beginDate = LocalDate.of(1899,12,31);
LocalDate today = LocalDate.now();
ChronoUnit.DAYS.between(beginDate, today)