Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate time difference between two dates using LocalDateTime in Java 8?

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.
like image 291
Harpreet Avatar asked Jul 07 '17 10:07

Harpreet


People also ask

How can we get duration between two dates or time in Java 8?

Parse both start_date and end_date from a string to produce the date, this can be done by using parse() method of the simpleDateFormat class. Find the time difference between two dates in milliseconds by using the method getTime() in Java as d2. getTime() – d1. getTime().

How do I find LocalDateTime between two dates?

We can use the simple isBefore , isAfter and isEqual to check if a date is within a certain date range; for example, the below program check if a LocalDate is within the January of 2020. startDate : 2020-01-01 endDate : 2020-01-31 testDate : 2020-01-01 testDate is within the date range.

How do I get time from LocalDateTime?

You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.

What is the difference between date and LocalDateTime in Java?

A Datetime is instead a physical concept (an instant of time), which in addition has a Timezone, and hence, it can be expressed in day/month/year. a LocalDateTime is just a bunch of numbers (day, month, year, hour, minute...) that represent a civil (not a physic) concept.


1 Answers

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");
}
like image 134
Robin Topper Avatar answered Sep 20 '22 02:09

Robin Topper