Java8和Java11中LocalDateTime到毫秒的差异

我目前正在将一些项目从 Java 8 升级到 Java 11,其中一个转换器的单元测试失败了。基本上问题源于由于先前通过 JDK 8 传递的日期精度导致的相等性检查失败。

这是测试的部分示例,为了清楚起见,我移动了转换器的内容:

@Test
public void testDateTime() {
    LocalDateTime expected = LocalDateTime.now().plusDays(1L);

    // converter contents
    long epochMillis = expected.atZone(ZoneId.systemDefault())
            .toInstant().toEpochMilli();
    LocalDateTime actual = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis),
            TimeZone.getDefault().toZoneId());


    assertThat(actual, equalTo(expected));
}

由于以下原因,这会导致资产错误:

Expected :<2021-06-02T14:06:21.820299>
Actual   :<2021-06-02T14:06:21.820>
Expected :<2021-06-02T14:06:21.820299>
Actual   :<2021-06-02T14:06:21.820>
Expected :<2021-06-02T14:06:21.820299>
Actual   :<2021-06-02T14:06:21.820>

我可以用 assertThat(actual, equalTo(expected.truncatedTo(ChronoUnit.MILLIS)))它们相等,但是,这意味着每次与被测试的转换器类进行比较(isAfter、isBefore、equals)时,都必须应用中继。

对于 JDK 11(或者我可能错过的文档:)),是否有正确的方法可以在LocalDateTimeto之间进行转换,Long反之亦然?


更新:

正如评论中所指出的,Java 8 和 11 的表示形式不同,因此导致测试失败。为了提供有关本文所要求内容的更多上下文,这里是测试验证的 2 种方法(我将其移至测试本身以仅捕获正在执行的内容,因为失败的单元测试属于使用实用方法)

public Long localDateTimeToEpochMillis(LocalDateTime ldt) {
    Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
        return ldt.atZone(ZoneId.systemDefault())
           .toInstant().toEpochMilli();
}

public LocalDateTime epochMillisToLocalDateTime(long epochMillis) {
    return LocalDateTime.ofInstant(
            Instant.ofEpochMilli(epochMillis), 
            ZoneId.systemDefault());
}

现有测试似乎验证的是,给定一个 long 值,我应该得到相同的 LocalDateTime 等效项,这是通过使用 Given(LocalDateTime 转换为 Long 值)然后返回 LocalDateTime 进行比较来完成的。

回答

如果你看一下区别:

您可以看到它删除了不到一毫秒的任何内容。

发生这种情况是因为您将 转换LocalDateTime为毫秒:

为了避免这种情况,您可以使用Instant#getNano

获取从秒开始沿时间线稍后的纳秒数。纳秒秒值测量从 getEpochSecond() 返回的秒算起的总纳秒数。

它可能看起来像这样:

.toInstant().toEpochMilli();

为什么它在 Java 8 中有效?

正如这篇文章和JDK-8068730提高 java.time.Clock.systemUTC() 的实现精度所描述的那样,Java 8 没有捕获小于一毫秒的时间单位。从 Java 9 开始,LocalDateTime.now(和类似的)以微秒为单位获得时间。

  • The current test is inaccurate. It’s a bit like comparing `double` values and converting one of them to an integer first. @Naman

以上是Java8和Java11中LocalDateTime到毫秒的差异的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>