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



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

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

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

但我得到这个:

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

法典:

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

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

很好,您找到了解决方案,我只想添加一些见解并对您的答案提出一些改进建议。

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

这是因为TimeZone.setDefault更改了整个JVM 的默认时区。在同一 JVM 中运行的任何其他应用程序都将受到它的影响。同一应用程序的其他部分也会受到影响,即使在多个线程中运行的相同代码也可能给您带来错误的结果(并且竞争条件难以调试)。

我注意到您正在使用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[欧洲/斯德哥尔摩]

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

这是代码。

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

最新更新