I want to convert a given date time (which is a utc date time) to the corresponding date time in CET with a proper mapping of the european summer/winter time switch (daylight saving time). I managed to to the opposite (CET to UTC) using java.time
:
public static LocalDateTime cetToUtc(LocalDateTime timeInCet) {
ZonedDateTime cetTimeZoned = ZonedDateTime.of(timeInCet, ZoneId.of("CET"));
return cetTimeZoned.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
}
But I fail to go the opposite way:
public static LocalDateTime utcToCet(LocalDateTime timeInUtc) {
ZonedDateTime cetTimeZoned = ZonedDateTime.of(timeInUtc,ZoneId.of("UTC"));
return cetTimeZoned.withZoneSameInstant(ZoneOffset.of(???)).toLocalDateTime(); // what to put here?
}
How can I do that?
ZoneId.of("CET")
?Zone
instead of anZoneOffset