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.
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().
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.
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.
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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With