【发布时间】:2017-03-27 09:33:22
【问题描述】:
我想将给定的日期时间(UTC 日期时间)转换为 CET 中的相应日期时间,并正确映射欧洲夏季/冬季时间开关(夏令时)。我使用java.time 设法相反(CET 到 UTC):
public static LocalDateTime cetToUtc(LocalDateTime timeInCet) {
ZonedDateTime cetTimeZoned = ZonedDateTime.of(timeInCet, ZoneId.of("CET"));
return cetTimeZoned.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
}
但我没有走相反的路:
public static LocalDateTime utcToCet(LocalDateTime timeInUtc) {
ZonedDateTime cetTimeZoned = ZonedDateTime.of(timeInUtc,ZoneId.of("UTC"));
return cetTimeZoned.withZoneSameInstant(ZoneOffset.of(???)).toLocalDateTime(); // what to put here?
}
我该怎么做?
【问题讨论】:
-
为什么不使用
ZoneId.of("CET")? -
@Jerry06 好的,谢谢,这行得通。我并没有真正意识到我也可以使用
Zone而不是ZoneOffset