java.time: Does the CET time zone considers daylight saving time?

前端 未结 2 1778
甜味超标
甜味超标 2021-02-05 14:36

I use the new java.time implementation of Java 8 and wonder about the output of a UTC to CET time conversion result.

ZonedDateTime utcTime = ZonedDa         


        
2条回答
  •  醉梦人生
    2021-02-05 15:00

    Because you know the offset and don't want to use DTS, why not use ZoneOffset.ofHours(1) method instead of ZoneId.of("CET")?

    Also you can call normalized() on any ZoneId instance to make it a fixed offset but sounds like less reliable than using an offset from the beginning.

    From ZoneId javadoc:

    A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime. There are two distinct types of ID:

    • Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses the same offset for all local date-times
    • Geographical regions - an area where a specific set of rules for finding the offset from UTC/Greenwich apply

    Most fixed offsets are represented by ZoneOffset. Calling normalized() on any ZoneId will ensure that a fixed offset ID will be represented as a ZoneOffset.

    If you're not using fixed offsets, then you're using geographical regions which means it depends on the region if DTS is observed or not. Same goes with PST. You'll see it observes DTS even though summer time is called PDT. Yes, it is confusing but this is how most tools work. Read the full ZoneId javadoc for a more thorough explanation (the Time-zone IDs section).

提交回复
热议问题