【问题标题】:How to make this Year-2038-problem junit test work as expected如何使这个 2038 年问题的 junit 测试按预期工作
【发布时间】:2019-05-21 08:41:27
【问题描述】:

我正在编写一些代码,这些代码将在 2038 年 1 月 19 日之后停止工作,因为 Year_2038_problem 当 java Date 溢出时,所以我想我可以创建一个在 2036 年开始失败的 junit 测试给我 2 年的时间来修复它。

为了先创建一个未通过的测试,我添加了 25 年,并且对测试没有失败感到惊讶。

@Test
public void warn2038Overflow() {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.YEAR, 25);

    // today is 2019 plus 25 years becomes 2044 which should overflow
    assertEquals("No calendar overflow", true,
            c.getTimeInMillis() > 0l);
}

任何线索为什么这不会失败?

我正在使用 Android-Studio-3.4.1 和 java-1.8.0_152(64 位)以及 'junit:junit:4.12' 和 'androidx.test:runner:1.1.0'

【问题讨论】:

    标签: java junit year2038


    【解决方案1】:

    Year-2038-problem 发生在您将纪元秒数存储在带符号的 32 位整数中(将在 2038 年溢出)。

    您使用的代码将毫秒数存储在有符号的 64 位整数中。这仍然可以正常工作for a while。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-29
      • 2014-11-22
      • 2020-02-09
      • 2020-04-23
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      • 2016-09-18
      相关资源
      最近更新 更多