-2

I was trying to get the hours between two local dates. And I used the code below :

 LocalDate startDate =  LocalDate.of(2000, 4, 30);

    long noOfHours = startDate.until(LocalTime.now(), ChronoUnit.HOURS);
    
    System.out.println(noOfHours);

I am having the error below :

Exception in thread "main" java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: 13:44:53.095819600 of type java.time.LocalTime
    at java.base/java.time.LocalDate.from(LocalDate.java:397)
    at java.base/java.time.LocalDate.until(LocalDate.java:1645)
    at BirthDay.App.main(App.java:36)
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    `LocalTime` does not have a Date component, it is only a time, e.g. "13:37:01" but _not_ "2021-07-10T13:37:01". And `LocalDate#until` simply does not support `HOURS` (have a look at the implementation). Why? Because `LocalDate` does not have a time component, only a date ("2000-04-30", but not "2000-04-30T00:00:00") – knittl Jul 10 '21 at 07:51
  • From [the docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDate.html#until(java.time.temporal.Temporal,java.time.temporal.TemporalUnit)): ***Throws:** `DateTimeException` - if the amount cannot be calculated, or the end temporal cannot be converted to a `LocalDate`*. The latter is the case here: there is no way to convert a `LocalTime` to a `LocalDate`. – Ole V.V. Jul 10 '21 at 09:25

2 Answers2

6

Error 1

As the exception states : you need coherent bound values, LocalTime has no day/month/year composants to be compared to a LocalDate

LocalDate startDate = LocalDate.of(2000, 4, 30);
long noOfHours = startDate.until(LocalTime.now(), ChronoUnit.HOURS);
// >> DateTimeException: Unable to obtain LocalDate from java.time.LocalTime

Error 2

Also you need composants that have the unit required by your ChronoUnit, using 2 LocalDate won't work with HOURS as they have none.

LocalDate startDate = LocalDate.of(2000, 4, 30);
long noOfHours = startDate.until(LocalDate.now(), ChronoUnit.HOURS);
// >> UnsupportedTemporalTypeException: Unsupported unit: Hours

Use LocalDateTime for that, to be coherent for both bounds and for unit time

LocalDateTime startDate = LocalDateTime.of(2000, 4, 30, 0, 0, 0);
long noOfHours = startDate.until(LocalDateTime.now(), ChronoUnit.HOURS);
System.out.println(noOfHours); // 185793
azro
  • 53,056
  • 7
  • 34
  • 70
5

Note that LocalDate has not time component, and LocalTime has no date component. So your code is trying to calculate the number of hours between 2000-04-30 (with no time specified), and the current local time (with no date specified). See the problem?

What you probably meant is,

How many hours are there, between today at the current local time, and 2000-04-30 at the start of that day (or some other time that day)?

This calculation needs two "date+time"s, not one date and one time. We represent that with LocalDateTime.

long noOfHours = startDate.atStartOfDay().until(LocalDateTime.now(), ChronoUnit.HOURS);

Note LocalDateTime.now() and atStartOfDay, which returns LocalDateTimes.

If you want to use another time at 2000-04-30, use startDate.atTime(...), or create a LocalDateTime in the first place, e.g.

LocalDateTime startDate =  LocalDateTime.of(2000, 4, 30, 12, 30);
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • @AhmedAjmineNehal If you think an answer answers your question, please consider accepting it by clicking on that checkmark! – Sweeper Jul 12 '21 at 08:21