Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds




Asked on December 02, 2018
I am trying to run plus(TemporalAmount amountToAdd) of LocalDate  class.


LocalDate localDate1 = LocalDate.now();

LocalDate localDate2 = localDate1.plus(Duration.ofDays(10));
System.out.println(localDate2);


But getting error.

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
at java.base/java.time.LocalDate.plus(LocalDate.java:1272)
at java.base/java.time.LocalDate.plus(LocalDate.java:139)
at java.base/java.time.Duration.addTo(Duration.java:1102)
at java.base/java.time.LocalDate.plus(LocalDate.java:1174)

Why?



Replied on December 02, 2018
Use Period instead of Duration.

LocalDate localDate2 = localDate1.plus(Period.ofDays(15));

And it will work.

Duration

Duration is most suitable in situations that measure machine-based time, such as code that uses an Instant object.


Period

To define an amount of time with date-based values (years, months, days), use the Period class.




Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us