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)。输出结果如下截图所示:

Epoch Converter

我非常确定,月份值之后的字段在我的代码中是错误的。任何建议都会很有帮助。

更新

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

更新

由于我从两个不同的网站得到了两个不同的结果,我想知道我的计算算法是否合法。如果我使用以下两个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。所以算法也需要处理负数。

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

430489 次浏览

不幸的是,似乎没有一个period类也可以跨越时间,所以您可能必须自己进行计算。

幸运的是,日期和时间类有很多实用程序方法,在某种程度上简化了这一点。这里有一个计算差异的方法,虽然不一定是最快的:

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.

基本的想法是这样的:创建一个临时的开始日期,并得到完整的年份结束。然后按年数调整该日期,使开始日期距离结束日期少于一年。按降序对每个时间单位重复上述操作。

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

我发现最好的方法是用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

塔帕斯玻色码和托马斯码存在一些问题。如果time differenс为负值,则array获得负值。例如,如果

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秒。

我建议在LocalDate和LocalTime实例上分离LocalDateTime实例。之后,我们可以获得Java 8 Period和Duration实例。Duration实例根据天数和全天的时间值(<24h),并随后修正周期值。当第二个LocalTime值在第一个LocalTime值之前时,需要将周期缩短一天。

下面是我计算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;
}

上述方法可用于计算任何本地日期和时间值的差值,例如:

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;
}

为上述方法编写单元测试很方便(它们都是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值之前或之前,所有测试都是成功的。

Groovy with中的@Thomas版本在列表中获取所需的单位,而不是硬编码这些值。这个实现(可以很容易地移植到Java -我将函数声明显式)使Thomas方法更具可重用性。

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 Kolomoets输入,代码返回23 Hours

当你提供一个单位列表时,它必须按单位的大小排序(最大的优先):

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

这里有一个使用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)));

对于你的问题,我有一个很简单的回答。它的工作原理。

import java.time.*;
import java.util.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;


public class MyClass {
public static void main(String args[]) {
DateTimeFormatter T = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
Scanner h = new Scanner(System.in);


System.out.print("Enter date of birth[dd/mm/yyyy hh:mm]: ");
String b = h.nextLine();


LocalDateTime bd = LocalDateTime.parse(b,T);
LocalDateTime cd = LocalDateTime.now();


long minutes = ChronoUnit.MINUTES.between(bd, cd);
long hours = ChronoUnit.HOURS.between(bd, cd);


System.out.print("Age is: "+hours+ " hours, or " +minutes+ " minutes old");
}
}

它应该更简单!

Duration.between(startLocalDateTime, endLocalDateTime).toMillis();

你可以把millis转换成任何你喜欢的单位:

String.format("%d minutes %d seconds",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

Joda-Time

由于许多答案需要API 26支持,我的最小API是23,我通过以下代码解决它:

import org.joda.time.Days


LocalDate startDate = Something
LocalDate endDate = Something
// The difference would be exclusive of both dates,
// so in most of use cases we may need to increment it by 1
Days.daysBetween(startDate, endDate).days

博士TL;

// get the calendar period between the times (years, months & days)
Period period = Period.between(start.toLocalDate(), end.toLocalDate());
// make sure to get the floor of the number of days
period = period.minusDays(end.toLocalTime().compareTo(start.toLocalTime()) >= 0 ? 0 : 1);


// get the remainder as a duration (hours, minutes, etc.)
Duration duration = Duration.between(start, end);
// remove days, already counted in the period
duration = duration.minusDays(duration.toDaysPart());

然后使用方法period.getYears()period.getMonths()period.getDays()duration.toHoursPart()duration.toMinutesPart()duration.toSecondsPart()

<一个href = " https://tio.run/ # # bVJNb9swDL3rVxAFNtiDpyZZ9xlku@SwwwIUSC / DsINiMYlTWwwkOUNQ9LenlOR4NlADhshHke@R1EGd1PuDfrxUzZGshwP70lcNyndzIcSx3dRVCWWtnIOVqgw8CeCvw51Xno8TVRoajmZrbyuz@ / MXlN25HJ4uv6hU9VJ5fOCa4T5zLGCEStpms8n0awHTaQGzL3x@5v9jPhfjbDT69dzZJOV@COenAu7u8iD@9hZ26MHvETiFs5WFI9qKNGzQ / 0 m0maa5vujyqxzgzv0bdrm / d / CWQ2eXi / uU06UuIPmyq5HFpqSnXliWF0HrGGJFrKdRjzyF1iJ46sVtayILtI2OaZsNRi@Qi540GbKpTOuWHMkGDGESWS5Lao7K4gONJaVoDt8XMIEf / H@DaZjPYDwWw / Y08yoHCnRrea9kINtTGwfCrB7ZQF / KXCyv8f7iAq7YeCxxEKl15qATxq4KULVFpc9QUms8aqjSKlKTYlD2ag4a7yFPwb9nmjheIdZn57GR1Hp55Hfot1l8rDdvNHSbZSstN5pJCxtdmyF67ZRthyUZ7W6K9OTTAnhkv0OtsOT/0CoWHWNRLCMxeyD6ZyBLqoshvkrUr0TWSUiKiHx@CRWfxfPlBQ" rel="noreferrer" title="Java (JDK) -在线试用">在线试用!< / >


扩大的答案

我将回答原来的问题,即如何得到两个LocalDateTimes之间的时间差年,月,日,小时和amp;分钟,这样quot;sum"(见下面的注释)的所有不同单位的值等于总时间差,这样每个单位的值比下一个大单位的值小,即。minutes < 60hours < 24,等等。

给出两个LocalDateTimes startend,例如:

LocalDateTime start = LocalDateTime.of(2019, 11, 28, 17, 15);
LocalDateTime end = LocalDateTime.of(2020, 11, 30, 16, 44);

我们可以用__abc0来表示两者之间的绝对时间跨度——也许可以使用Duration.between(start, end)。但是我们可以从Duration中提取的最大单位是天(作为一个时间单位,相当于24小时)-请参阅下面的说明以获得解释。为了使用更大的单位(月,年),我们可以用一对(PeriodDuration)来表示这个Duration,其中Period测量精确到天的差值,而Duration表示余数。

要获得Period:

Period period = Period.between(start.toLocalDate(), end.toLocalDate());

这里我们需要小心,因为Period实际上是一个日期差异,而不是一个时间量,并且它的所有计算都是基于日历日期(参见下面的部分)。例如,从2000年1月1日23:59到2000年1月2日00:01,Period会说有1天的差值,因为这是两个日期之间的差值,即使时间增量小于24h。因此,如果结束datetime上的时间早于开始datetime上的时间,我们需要从日期计数中减去1,因为它不对应于一个完整的24小时范围(对应的余数将由Duration记录):

period = period.minusDays(end.toLocalTime().compareTo(start.toLocalTime()) >= 0 ? 0 : 1);

要获得余数,作为Duration:

Duration duration = Duration.between(start, end);

由于精确到天的差异已经被我们的周期考虑在内,我们只需要保留更小的单位(小时,分钟等):

duration = duration.minusDays(duration.toDaysPart()); // essentially "duration (mod 1 day)"

现在我们可以简单地使用PeriodDuration中定义的方法来提取单个单元:

System.out.printf(
"%d years, %d months, %d days, %d hours, %d minutes, %d seconds",
period.getYears(), period.getMonths(), period.getDays(),
duration.toHoursPart(), duration.toMinutesPart(), duration.toSecondsPart()
);
1 years, 0 months, 1 days, 23 hours, 29 minutes, 0 seconds

或者,使用默认格式:

System.out.println(period + " + " + duration);
P1Y1D + PT23H29M

年、月及年注;天

注意,在java.time的概念中,period "units"像“month"或“;year"不要表示一个固定的、绝对的时间值——它们依赖于日期和日历,如下例所示:

LocalDateTime
start1 = LocalDateTime.of(2020, 1, 1, 0, 0),
end1 = LocalDateTime.of(2021, 1, 1, 0, 0),
start2 = LocalDateTime.of(2021, 1, 1, 0, 0),
end2 = LocalDateTime.of(2022, 1, 1, 0, 0);
System.out.println(Period.between(start1.toLocalDate(), end1.toLocalDate()));
System.out.println(Duration.between(start1, end1).toDays());
System.out.println(Period.between(start2.toLocalDate(), end2.toLocalDate()));
System.out.println(Duration.between(start2, end2).toDays());
P1Y
366
P1Y
365

另一个例子是,从2000年1月1日23:59到2000年1月2日00:01,Period会说有一个1天的差值,因为这是两个日期之间的差值,即使时间增量小于24h。

五年多后,我回答了我的问题。我认为持续时间为负的问题可以通过简单的修正来解决:

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


Period period = Period.between(fromDateTime.toLocalDate(), toDateTime.toLocalDate());
Duration duration = Duration.between(fromDateTime.toLocalTime(), toDateTime.toLocalTime());


if (duration.isNegative()) {
period = period.minusDays(1);
duration = duration.plusDays(1);
}
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);
long time[] = {hours, minutes, secs};
System.out.println(period.getYears() + " years "
+ period.getMonths() + " months "
+ period.getDays() + " days "
+ time[0] + " hours "
+ time[1] + " minutes "
+ time[2] + " seconds.");

注意:站点https://www.epochconverter.com/date-difference现在正确计算时间差。

谢谢大家的讨论和建议。

对于试图计算两个LocalTime对象之间的分钟数的任何人,请记住结果将是- 1。

例如,

long countMins = MINUTES.between( LocalTime.of(8,00),LocalTime.of(9,59) );
System.out.println("countMins = " + countMins );

将给出Output: countMins = 119 而不是120