如何在Java 8中使用LocalDateTime计算两个日期之间的时差? [英] How to calculate time difference between two dates using LocalDateTime in Java 8?
问题描述
给出了许多答案,但是我找不到与我的情况相符的答案。我还需要找到8小时的时间差异以及日期变更。就像如果时间大于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 .
我们有任何方法可以在 LocalDateTime $ c中实现相同的效果吗? $ c>在Java-8中?
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.
推荐答案
以下显示 2
,就像您期望的那样。
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.
要做当差异少于8小时时,您可以执行以下操作:
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");
}
这篇关于如何在Java 8中使用LocalDateTime计算两个日期之间的时差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!