博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 比较2个时间大小写_date - Java 8:计算两个LocalDateTime之间的差异
阅读量:6360 次
发布时间:2019-06-23

本文共 4593 字,大约阅读时间需要 15 分钟。

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

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

这是我计算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 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值之前,所有测试都是成功的。

转载地址:http://gcima.baihongyu.com/

你可能感兴趣的文章
Centos6 安装cdh5.7
查看>>
Outlook 2010添加Exchange Online用户
查看>>
VSS6.0 admin密码清除
查看>>
git status遇到old mode问题
查看>>
字符,字节和编码 一
查看>>
强制关闭数据库
查看>>
502错误详解
查看>>
ARM开发板 嵌入式Linux 修改开机启动LOGO
查看>>
Baby-gin
查看>>
Docker实践(2)—虚拟网络
查看>>
C#实现多线程的方法:线程(Thread类)和线程池(ThreadPool)
查看>>
Ubuntu安装pintos
查看>>
看这里,教你如何快速将pdf文件翻译成中文
查看>>
开源Linux监控系统:Icinga
查看>>
Android模拟器检测常用方法
查看>>
sqlite 中判断某个表是否存在的方法
查看>>
历史数据的清理方法
查看>>
rrdtool学习和自定义脚本绘制图形备忘
查看>>
LayuI固定块关闭
查看>>
linux基础命令(4)
查看>>