Duration.ofDays generates UnsupportedTemporalTypeException

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

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

the same as in JodaTime


//(year,month,day)

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

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.)

However, to get the result I was after was as simple as adding the LocalDate method "atStartOfDay" to each of my objects.

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.

Hope this helps someone else.