Java 8:计算两个LocalDateTime之间的差异

我正在计算两个LocalDateTime之间的差异。

输出需要格式为y years m months d days h hours m minutes s seconds 。 这是我写的:

 import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; import java.time.Period; import java.time.ZoneId; public class Main { static final int MINUTES_PER_HOUR = 60; static final int SECONDS_PER_MINUTE = 60; static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; public static void main(String[] args) { LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 9, 19, 46, 45); LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); Period period = getPeriod(fromDateTime, toDateTime); long time[] = getTime(fromDateTime, toDateTime); System.out.println(period.getYears() + " years " + period.getMonths() + " months " + period.getDays() + " days " + time[0] + " hours " + time[1] + " minutes " + time[2] + " seconds."); } private static Period getPeriod(LocalDateTime dob, LocalDateTime now) { return Period.between(dob.toLocalDate(), now.toLocalDate()); } private static long[] getTime(LocalDateTime dob, LocalDateTime now) { LocalDateTime today = LocalDateTime.of(now.getYear(), now.getMonthValue(), now.getDayOfMonth(), dob.getHour(), dob.getMinute(), dob.getSecond()); Duration duration = Duration.between(today, now); long seconds = duration.getSeconds(); long hours = seconds / SECONDS_PER_HOUR; long minutes = ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE); long secs = (seconds % SECONDS_PER_MINUTE); return new long[]{hours, minutes, secs}; } } 

我得到的输出是29 years 8 months 24 days 12 hours 0 minutes 50 seconds 。 我检查了我的结果从这个网站 (值12/16/1984 07:45:5509/09/2014 19:46:45 )。 以下屏幕截图显示了输出:

时代转换器

我很确定月份值后面的字段来自我的代码错误。 任何build议将是非常有益的。

更新

我从另一个网站testing了我的结果,我得到的结果是不同的。 这里是: 计算两个date (结果:29年,8个月,24天,12小时,0分50秒) 之间的持续时间 。

更新

由于我得到了两个不同的网站的两个不同的结果,我想知道我的计算algorithm是否合法。 如果我使用以下两个LocalDateTime对象:

 LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); 

然后输出: 29 years 8 months 25 days -1 hours -5 minutes -10 seconds.

从这个链接应该是29 years 8 months 24 days 22 hours, 54 minutes and 50 seconds 。 所以algorithm也需要处理负数。

注意这个问题不是关于哪个网站给了我什么结果,我需要知道正确的algorithm,并需要有正确的结果。

不幸的是,似乎还没有跨越时间的阶段,所以你可能必须自己做计算。

幸运的是date和时间类有很多实用的方法可以在一定程度上简化。 以下是计算差异的方法,虽然不一定是最快的:

 LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); 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( years + " years " + months + " months " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); //prints: 29 years 8 months 24 days 22 hours 54 minutes 50 seconds. 

基本的想法是:创build一个临时的开始date,并把整整一年的时间拖到最后。 然后调整这个date的年数,以便开始date是从一年结束小于一年。 重复每个时间单位降序。

最后一个免责声明:我没有考虑不同的时区(两个date应该在同一个时区),我也没有testing/检查日历中的夏令时或其他更改(如萨摩亚的时区更改)影响这个计算。 所以小心使用。

我发现最好的办法是用ChronoUnit。

 long minutes = ChronoUnit.MINUTES.between(fromDate, toDate); long hours = ChronoUnit.HOURS.between(fromDate, toDate); 

其他文档在这里: https : //docs.oracle.com/javase/tutorial/datetime/iso/period.html

这里有一个使用Duration和TimeUnit来获取'hh:mm:ss'格式的例子。

  Duration dur = Duration.between(LocalDateTimeIni, LocalDateTimeEnd); long millis = dur.toMillis(); String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 

而且Groovy中的@Thomas版本在列表中取得了所需的单位,而不是对这些值进行硬编码。 这个实现(可以很容易地移植到Java – 我做了明确的函数声明)使托马斯的方法更加可重用。

 def fromDateTime = LocalDateTime.of(1968, 6, 14, 0, 13, 0) def toDateTime = LocalDateTime.now() def listOfUnits = [ ChronoUnit.YEARS, ChronoUnit.MONTHS, ChronoUnit.DAYS, ChronoUnit.HOURS, ChronoUnit.MINUTES, ChronoUnit.SECONDS, ChronoUnit.MILLIS] println calcDurationInTextualForm(listOfUnits, fromDateTime, toDateTime) String calcDurationInTextualForm(List<ChronoUnit> listOfUnits, LocalDateTime ts, LocalDateTime to) { def result = [] listOfUnits.each { chronoUnit -> long amount = ts.until(to, chronoUnit) ts = ts.plus(amount, chronoUnit) if (amount) { result << "$amount ${chronoUnit.toString()}" } } result.join(', ') } 

在撰写本文时,上面的代码返回47 Years, 8 Months, 9 Days, 22 Hours, 52 Minutes, 7 Seconds, 140 Millis 。 而且,对于@Gennady Kolomoetsinput,代码返回23 Hours

当你提供单位清单时,必须按单位大小sorting(最大的一个):

 def listOfUnits = [ChronoUnit.WEEKS, ChronoUnit.DAYS, ChronoUnit.HOURS] // returns 2495 Weeks, 3 Days, 8 Hours 

Tapas Bose代码和Thomas代码有一些问题。 如果时间差是负数,数组会得到负值。 例如,如果

 LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 46, 45); LocalDateTime fromDateTime = LocalDateTime.of(2014, 9, 9, 7, 46, 45); 

它返回0年0个月1天-1小时0分0秒。

我认为正确的输出是:0年0个月0天23小时0分0秒。

我build议分开LocalDate和LocalTime实例上的LocalDateTime实例。 之后,我们可以获得Java 8 Period和Duration实例。 持续时间实例在天数和全天时间值(<24小时)之间分开,并随后修正周期值。 当第二个LocalTime值在firstLocalTime值之前时,有必要减less一天的时间。

以下是我计算LocalDateTime差异的方法:

 private void getChronoUnitForSecondAfterFirst(LocalDateTime firstLocalDateTime, LocalDateTime secondLocalDateTime, long[] chronoUnits) { /*Separate LocaldateTime on LocalDate and LocalTime*/ LocalDate firstLocalDate = firstLocalDateTime.toLocalDate(); LocalTime firstLocalTime = firstLocalDateTime.toLocalTime(); LocalDate secondLocalDate = secondLocalDateTime.toLocalDate(); LocalTime secondLocalTime = secondLocalDateTime.toLocalTime(); /*Calculate the time difference*/ Duration duration = Duration.between(firstLocalDateTime, secondLocalDateTime); long durationDays = duration.toDays(); Duration throughoutTheDayDuration = duration.minusDays(durationDays); Logger.getLogger(PeriodDuration.class.getName()).log(Level.INFO, "Duration is: " + duration + " this is " + durationDays + " days and " + throughoutTheDayDuration + " time."); Period period = Period.between(firstLocalDate, secondLocalDate); /*Correct the date difference*/ if (secondLocalTime.isBefore(firstLocalTime)) { period = period.minusDays(1); Logger.getLogger(PeriodDuration.class.getName()).log(Level.INFO, "minus 1 day"); } Logger.getLogger(PeriodDuration.class.getName()).log(Level.INFO, "Period between " + firstLocalDateTime + " and " + secondLocalDateTime + " is: " + period + " and duration is: " + throughoutTheDayDuration + "\n-----------------------------------------------------------------"); /*Calculate chrono unit values and write it in array*/ chronoUnits[0] = period.getYears(); chronoUnits[1] = period.getMonths(); chronoUnits[2] = period.getDays(); chronoUnits[3] = throughoutTheDayDuration.toHours(); chronoUnits[4] = throughoutTheDayDuration.toMinutes() % 60; chronoUnits[5] = throughoutTheDayDuration.getSeconds() % 60; } 

上面的方法可以用来计算任何本地date和时间值的差异,例如:

 public long[] getChronoUnits(String firstLocalDateTimeString, String secondLocalDateTimeString) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime firstLocalDateTime = LocalDateTime.parse(firstLocalDateTimeString, formatter); LocalDateTime secondLocalDateTime = LocalDateTime.parse(secondLocalDateTimeString, formatter); long[] chronoUnits = new long[6]; if (secondLocalDateTime.isAfter(firstLocalDateTime)) { getChronoUnitForSecondAfterFirst(firstLocalDateTime, secondLocalDateTime, chronoUnits); } else { getChronoUnitForSecondAfterFirst(secondLocalDateTime, firstLocalDateTime, chronoUnits); } return chronoUnits; } 

为上面的方法编写unit testing很方便(他们都是PeriodDuration类的成员)。 代码如下:

 @RunWith(Parameterized.class) public class PeriodDurationTest { private final String firstLocalDateTimeString; private final String secondLocalDateTimeString; private final long[] chronoUnits; public PeriodDurationTest(String firstLocalDateTimeString, String secondLocalDateTimeString, long[] chronoUnits) { this.firstLocalDateTimeString = firstLocalDateTimeString; this.secondLocalDateTimeString = secondLocalDateTimeString; this.chronoUnits = chronoUnits; } @Parameters public static Collection<Object[]> periodValues() { long[] chronoUnits0 = {0, 0, 0, 0, 0, 0}; long[] chronoUnits1 = {0, 0, 0, 1, 0, 0}; long[] chronoUnits2 = {0, 0, 0, 23, 0, 0}; long[] chronoUnits3 = {0, 0, 0, 1, 0, 0}; long[] chronoUnits4 = {0, 0, 0, 23, 0, 0}; long[] chronoUnits5 = {0, 0, 1, 23, 0, 0}; long[] chronoUnits6 = {29, 8, 24, 12, 0, 50}; long[] chronoUnits7 = {29, 8, 24, 12, 0, 50}; return Arrays.asList(new Object[][]{ {"2015-09-09 21:46:44", "2015-09-09 21:46:44", chronoUnits0}, {"2015-09-09 21:46:44", "2015-09-09 22:46:44", chronoUnits1}, {"2015-09-09 21:46:44", "2015-09-10 20:46:44", chronoUnits2}, {"2015-09-09 21:46:44", "2015-09-09 20:46:44", chronoUnits3}, {"2015-09-10 20:46:44", "2015-09-09 21:46:44", chronoUnits4}, {"2015-09-11 20:46:44", "2015-09-09 21:46:44", chronoUnits5}, {"1984-12-16 07:45:55", "2014-09-09 19:46:45", chronoUnits6}, {"2014-09-09 19:46:45", "1984-12-16 07:45:55", chronoUnits6} }); } @Test public void testGetChronoUnits() { PeriodDuration instance = new PeriodDuration(); long[] expResult = this.chronoUnits; long[] result = instance.getChronoUnits(this.firstLocalDateTimeString, this.secondLocalDateTimeString); assertArrayEquals(expResult, result); } 

}

无论第一个LocalDateTime的值是否在任何LocalTime值之前,所有testing都成功。