11

There are numerous answers given but I am unable to find compatible with my situation. I need to find difference of 8 hours in time as well as on date change too. Like if time is greater then 8 hours then do not execute something .

Do we have any method which achieve the same in LocalDateTime in Java-8?

I have tried to use following but unable to achieve the same.

LocalDateTime fromDateTime = LocalDateTime.of(2017, 07, 07, 07, 00, 55);
LocalDateTime toDateTime = LocalDateTime.now();
LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);

long years = tempDateTime.until(toDateTime, ChronoUnit.YEARS);
tempDateTime = tempDateTime.plusYears(years);

long months = tempDateTime.until(toDateTime, ChronoUnit.MONTHS);
tempDateTime = tempDateTime.plusMonths(months);

long days = tempDateTime.until(toDateTime, ChronoUnit.DAYS);
tempDateTime = tempDateTime.plusDays(days);

long hours = tempDateTime.until(toDateTime, ChronoUnit.HOURS);
tempDateTime = tempDateTime.plusHours(hours);

long minutes = tempDateTime.until(toDateTime, ChronoUnit.MINUTES);
tempDateTime = tempDateTime.plusMinutes(minutes);

long seconds = tempDateTime.until(toDateTime, ChronoUnit.SECONDS);

System.out.println("" + java.time.Duration.between(tempDateTime, toDateTime).toHours());

System.out.println(years + " years "
        + months + " months "
        + days + " days "
        + hours + " hours "
        + minutes + " minutes "
        + seconds + " seconds.");

It is difficult to check on time and date separately.

Initially I coded it like but it does not looks correct:

return openBrat!=null 
        && openBrat.until(LocalDateTime.now(), ChronoUnit.DAYS) == 0 &&  
          openBrat.until(LocalDateTime.now(), ChronoUnit.HOURS) >= 8
         && openBrat.until(LocalDateTime.now(), ChronoUnit.Minutes) >= 0;

Could anyone please suggest how to subtract like:

2017 07 06 23:30:00 - 2017 07 07 01:30:00 - Should return 2 hours.
Harpreet
  • 186
  • 1
  • 1
  • 10
  • 2
    You can also use `ChronoUnit.HOURS.between(date1, date2)` –  Jul 07 '17 at 11:32
  • 3
    Are you aware that `LocalDateTime` purposely has no concept of time zone? So using that class means you will be ignoring any anomalies such as Daylight Saving Time (DST). If you want to account for such realities, use `ZonedDateTime`. – Basil Bourque Jul 07 '17 at 13:50
  • Possible duplicate of [Java 8: Calculate difference between two LocalDateTime](https://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime) – rogerdpack Dec 08 '17 at 17:38

2 Answers2

19

The following prints 2, just like you'd expect.

LocalDateTime ldt1 = LocalDateTime.of(2017, 07, 06, 23, 30, 00);
LocalDateTime ldt2 = LocalDateTime.of(2017, 07, 07, 1, 30, 00);
System.out.println(Duration.between(ldt1, ldt2).toHours());

There is nothing wrong with your code. If you don't get the outcome you expect, you may need to check if your expectation is correct.


To do something when the difference is less than 8 hours, you can do something like this:

LocalDateTime ldt1 = LocalDateTime.of(2017, 07, 06, 23, 30, 00);
LocalDateTime ldt2 = LocalDateTime.of(2017, 07, 07, 1, 30, 00);
Duration d1 = Duration.between(ldt1, ldt2);
Duration d2 = Duration.ofHours(8);

if (d1.compareTo(d2) > 0) {
    System.out.println("do nothing");
} else {
    System.out.println("do something");
}
Robin Topper
  • 2,295
  • 1
  • 17
  • 25
  • How can we check it on hours minutes and seconds? Because condition check is that if time is more then 8 hours then execute something so = and > works on toHours but not minutes and seconds. – Harpreet Jul 07 '17 at 10:33
  • 2
    @Harpreet So just get the duration in seconds, or milliseconds, or whatever you need, and check if it is more than 8 hours worth of seconds (or milliseconds, etc.). – David Conrad Jul 07 '17 at 10:37
  • @Harpreet Updated my answer with a bit of extra code – Robin Topper Jul 07 '17 at 11:10
10

My understanding is that you want to get the difference between those 2 dates and "break" it in terms of how many hours, minutes and seconds this difference is.

You can use the Duration class as already explained. But the Duration calculates the difference in seconds and nanoseconds, so you'll have to do some math to get this amount of seconds in separate fields:

LocalDateTime d1 = LocalDateTime.of(2017, 7, 6, 23, 30, 0);
LocalDateTime d2 = LocalDateTime.of(2017, 7, 7, 7, 0, 55);

Duration duration = Duration.between(d1, d2);
// total seconds of difference (using Math.abs to avoid negative values)
long seconds = Math.abs(duration.getSeconds());
long hours = seconds / 3600;
seconds -= (hours * 3600);
long minutes = seconds / 60;
seconds -= (minutes * 60);
System.out.println(hours + " hours " + minutes + " minutes " + seconds + " seconds");

In Java 9 and later, you can call the new Duration::to…Part methods to get number of days, hours, minutes, or seconds rather than calculate the numbers yourself.

The output will be:

7 hours 30 minutes 55 seconds

And the variables hours, minutes and seconds will have the respective values of 7, 30 and 55. If you also want the nanoseconds, just call duration.getNano() to get the respective value (in the example above, it's 0).

If I test with different values:

LocalDateTime d1 = LocalDateTime.of(2017, 7, 6, 23, 30, 0);
LocalDateTime d2 = LocalDateTime.of(2017, 7, 7, 1, 30, 0);

The result will be:

2 hours 0 minutes 0 seconds


If you just want the difference in hours, you can use:

ChronoUnit.HOURS.between(d1, d2);

You can optionally use Math.abs to avoid negative values.

This will return the difference in hours, ignoring the remaining minutes and seconds: in the first example (d2 = LocalDateTime.of(2017, 7, 7, 7, 0, 55)) it will return 7 and in the second example (d2 = LocalDateTime.of(2017, 7, 7, 1, 30, 0)) it will return 2.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154