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

前端 未结 2 1772
甜味超标
甜味超标 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 14:45

    The IANA definition of CET is that it follows the time-zone rules of Central Europe, which includes both winter and summer time. The rules can be seen here, which shows that "CET" is based on "C-Eur" which includes summer time.

    In java.time you can also see the full set of rules:

    ZoneId zone = ZoneId.of("CET");
    System.out.println(zone);
    System.out.println(zone.getRules());
    for (ZoneOffsetTransition trans : zone.getRules().getTransitions()) {
      System.out.println(trans);
    }
    for (ZoneOffsetTransitionRule rule : zone.getRules().getTransitionRules()) {
      System.out.println(rule);
    }
    

    which prints:

    CET
    ZoneRules[currentStandardOffset=+01:00]
    Transition[Gap at 1916-04-30T23:00+01:00 to +02:00]
    Transition[Overlap at 1916-10-01T01:00+02:00 to +01:00]
    Transition[Gap at 1917-04-16T02:00+01:00 to +02:00]
    Transition[Overlap at 1917-09-17T03:00+02:00 to +01:00]
    Transition[Gap at 1918-04-15T02:00+01:00 to +02:00]
    Transition[Overlap at 1918-09-16T03:00+02:00 to +01:00]
    Transition[Gap at 1940-04-01T02:00+01:00 to +02:00]
    Transition[Overlap at 1942-11-02T03:00+02:00 to +01:00]
    Transition[Gap at 1943-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1943-10-04T03:00+02:00 to +01:00]
    Transition[Gap at 1944-04-03T02:00+01:00 to +02:00]
    Transition[Overlap at 1944-10-02T03:00+02:00 to +01:00]
    Transition[Gap at 1945-04-02T02:00+01:00 to +02:00]
    Transition[Overlap at 1945-09-16T03:00+02:00 to +01:00]
    Transition[Gap at 1977-04-03T02:00+01:00 to +02:00]
    Transition[Overlap at 1977-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1978-04-02T02:00+01:00 to +02:00]
    Transition[Overlap at 1978-10-01T03:00+02:00 to +01:00]
    Transition[Gap at 1979-04-01T02:00+01:00 to +02:00]
    Transition[Overlap at 1979-09-30T03:00+02:00 to +01:00]
    Transition[Gap at 1980-04-06T02:00+01:00 to +02:00]
    Transition[Overlap at 1980-09-28T03:00+02:00 to +01:00]
    Transition[Gap at 1981-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1981-09-27T03:00+02:00 to +01:00]
    Transition[Gap at 1982-03-28T02:00+01:00 to +02:00]
    Transition[Overlap at 1982-09-26T03:00+02:00 to +01:00]
    Transition[Gap at 1983-03-27T02:00+01:00 to +02:00]
    Transition[Overlap at 1983-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1984-03-25T02:00+01:00 to +02:00]
    Transition[Overlap at 1984-09-30T03:00+02:00 to +01:00]
    Transition[Gap at 1985-03-31T02:00+01:00 to +02:00]
    Transition[Overlap at 1985-09-29T03:00+02:00 to +01:00]
    Transition[Gap at 1986-03-30T02:00+01:00 to +02:00]
    Transition[Overlap at 1986-09-28T03:00+02:00 to +01:00]
    Transition[Gap at 1987-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1987-09-27T03:00+02:00 to +01:00]
    Transition[Gap at 1988-03-27T02:00+01:00 to +02:00]
    Transition[Overlap at 1988-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1989-03-26T02:00+01:00 to +02:00]
    Transition[Overlap at 1989-09-24T03:00+02:00 to +01:00]
    Transition[Gap at 1990-03-25T02:00+01:00 to +02:00]
    Transition[Overlap at 1990-09-30T03:00+02:00 to +01:00]
    Transition[Gap at 1991-03-31T02:00+01:00 to +02:00]
    Transition[Overlap at 1991-09-29T03:00+02:00 to +01:00]
    Transition[Gap at 1992-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1992-09-27T03:00+02:00 to +01:00]
    Transition[Gap at 1993-03-28T02:00+01:00 to +02:00]
    Transition[Overlap at 1993-09-26T03:00+02:00 to +01:00]
    Transition[Gap at 1994-03-27T02:00+01:00 to +02:00]
    Transition[Overlap at 1994-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1995-03-26T02:00+01:00 to +02:00]
    Transition[Overlap at 1995-09-24T03:00+02:00 to +01:00]
    Transition[Gap at 1996-03-31T02:00+01:00 to +02:00]
    Transition[Overlap at 1996-10-27T03:00+02:00 to +01:00]
    Transition[Gap at 1997-03-30T02:00+01:00 to +02:00]
    Transition[Overlap at 1997-10-26T03:00+02:00 to +01:00]
    TransitionRule[Gap +01:00 to +02:00, SUNDAY on or after MARCH 25 at 02:00 STANDARD, standard offset +01:00]
    TransitionRule[Overlap +02:00 to +01:00, SUNDAY on or after OCTOBER 25 at 02:00 STANDARD, standard offset +01:00]
    

    The key here is to understand that the time-zone identifier and the "short name" of that identifier are two different elements. The identifier is always fixed as "CET", but the name changes between "CET" and "CEST".

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题