使用LocalDateTIme和ZonedDateTime从日期减去一天后获得错误的结果

时间:2017-02-22 08:18:37

标签: java dst java-time zoneddatetime

我正在使用"2016-03-28T02:00:00+0200"(UTC秒中的1459123200)进行测试

减去1天后,应用DST并输出:

  

" 2016-03-27T03:00:00 + 0200"

但我得到了这个:

  

2016-03-26T01:00 + 01:00 [欧洲/斯德哥尔摩]

CODE:

public class DateFormatSampleCode {
    public static void main(String[] args) 
    {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(1459123200, 0, ZoneOffset.UTC);

        System.out.println(localDateTime);
        localDateTime = localDateTime.minusDays(1);
        System.out.println(localDateTime);

        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("Europe/Stockholm"));

        System.out.println(zonedDateTime);
    }
}

请检查并指出我出错的地方。

2 个答案:

答案 0 :(得分:1)

我想我可以回答上面的问题。

这是代码。

public ZonedDateTime addDays(long myUTCTimeInSeconds, int days) {
    Instant instant = Instant.ofEpochSecond(myUTCTimeInSeconds);
    ZonedDateTime dateTimeWithOffSet = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
    if (localDays >= 0) {
        dateTimeWithOffSet = dateTimeWithOffSet.plusDays(localDays);
    } else {
        dateTimeWithOffSet = dateTimeWithOffSet.minusDays(abs(localDays));
    }
    return dateTimeWithOffSet;
}

如果时区与系统的时区不同,我们可以设置默认时区,并在调用上述方法后将时区重置为:

TimeZone systemDefaultTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone(timezone));

addDays(1459123200, 1);
TimeZone.setDefault(systemDefaultTimeZone);

答案 1 :(得分:1)

您找到了解决方案,我只想添加一些见解并建议your answer稍微改进一下。

使用TimeZone.setDefault设置JVM默认时区不是实现此目的的最佳方法。虽然它可能在大多数时候都有效,但如果您认为此代码在更复杂的环境中运行,则会有一点风险并且容易出错。

这是因为TimeZone.setDefault更改了整个 JVM的默认时区。在同一JVM中运行的任何其他应用程序都将受其影响。同一应用程序的其他部分也会受到影响,即使在多个线程中运行的相同代码也可能会给您错误的结果(以及race conditions are hard to debug)。

我注意到你正在使用TimeZone.setDefault(TimeZone.getTimeZone(timezone));。这意味着您已经在使用特定时区,因此无需依赖JVM的默认值。如果您有特定的时区名称,只需使用它而不是默认名称。所以我建议你addDays方法应该是这样的:

public ZonedDateTime addDays(long myUTCTimeInSeconds, int days, String timezone) {
    // get the instant from the UTC seconds
    Instant instant = Instant.ofEpochSecond(myUTCTimeInSeconds);
    // get the instant at the specified timezone
    ZonedDateTime z = instant.atZone(ZoneId.of(timezone));

    // add days
    return z.plusDays(days);
}

所做的改进:

    如果您将plusDays传递给它,则
  • -1已扣减1天。无需检查值并使用abs方法。
  • 不要使用JVM默认时区:而不是ZoneId.systemDefault(),请使用您已有的timezone(您在setDefault方法中使用的那个)
  • instant.atZone相当于ZonedDateTime.ofInstant。 IMO,atZone更“可读”,但在这种情况下,这是一个选择和代码风格的问题。它对最终结果没有影响。

有了这个,你可以这样做:

// call directly, no need to change the default timezone
System.out.println(addDays(1459123200, -1, "Europe/Stockholm"));

这将打印:

  

2016-03-27T03:00 + 02:00 [欧洲/斯德哥尔摩]