Duration.ofDays throws UnsupportedTemporalTypeException

I am trying to learn a new date and time API . My code works, except for the last line:

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 to add Duration on days, it generates an error. Can someone help me understand why?

Mistake:

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)    
+4
source share
2 answers

A Duration measures time using time values ​​(seconds, nanoseconds). A period uses dates (years, months, days). here's the link

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

same as in JodaTime

+9
source

, , .

, Period LocalDate. (, , , .)

, , , LocalDate "atStartOfDay" .

, :

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

:

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

LocalDateTime, Duration. "" , .

, - .

+2

Source: https://habr.com/ru/post/1621499/


All Articles