Java API 获取一年中特定时区夏令时的开始和结束详细信息

Java API to get the start and end detail of Daylight saving time for a particular timezone for a year

所以我正在使用 getAvailableZoneIds() method under java.time.ZoneId 获取可用时区的列表。

我想知道是否有一种方法可以针对特定时区,例如-“America/Chicago”,我可以找出一年夏令时的开始和结束日期时间详细信息。 比如什么时候开始什么时候结束。 虽然各种 类 已经消失,包括 java 中可用的 ZoneId、ZonedDateTime、ZoneOffset、TimeZone,但无法找到任何方法来获取这些详细信息。

我尝试了下面的代码并给出了下面提到的输出

    ZoneId zoneId= ZoneId.of("America/Sao_Paulo");
    ZoneRules zoneRules = zoneId.getRules();
    System.out.println("previous Transition of DST ==>  " + zoneRules.previousTransition(Instant.now()));
    System.out.println("next Transition of DST ==>  " + zoneRules.nextTransition(Instant.now()));

输出:

previous Transition of DST ==>  Transition[Overlap at 2019-02-17T00:00-02:00 to -03:00]
next Transition of DST ==>  Transition[Gap at 2019-11-03T00:00-03:00 to -02:00]

但我需要找出特定年份的开始时间和结束时间。

对于特定年份,您可以给出 ZoneRules a year as part of specifying a moment. For any given moment, you can ask next or previous ZoneOffsetTransition

这里是 Europe/Sofia 时区的示例代码。

ZoneId zoneSofia = ZoneId.of( "Europe/Sofia" );
ZoneRules zoneRules = zoneSofia.getRules();

// Pick a moment, arbitrarily.
ZonedDateTime zdt = ZonedDateTime.of( 2019 , 10 , 15 , 10 , 0 , 0 , 0 , zoneSofia );
// Is DST in effect at that moment?
boolean isDst = zoneRules.isDaylightSavings( zdt.toInstant() );

// When are the closest offset transitions, previous (in the past), and next (in the future).
ZoneOffsetTransition previousTransition = zoneRules.previousTransition( zdt.toInstant() );
ZoneOffsetTransition nextTransition = zoneRules.nextTransition( zdt.toInstant() );

// When is the next transition happening in UTC? In Sofia time?
Instant nextTransitionInstant = nextTransition.getInstant();  // An `Instant`` is always in UTC, by definition.
ZonedDateTime nextTransactionZdt = nextTransitionInstant.atZone( zoneSofia ); // Same moment, same point on the timeline, different wall-clock time.
boolean isDstAfterTransition = zoneRules.isDaylightSavings( nextTransactionZdt.toInstant() );

转储到控制台。

System.out.println( "zone = " + zoneSofia );
System.out.println( "zdt: " + zdt );
System.out.println( "isDst: " + isDst );
System.out.println( "previousTransition = " + previousTransition );
System.out.println( "nextTransition = " + nextTransition );
System.out.println( "nextTransitionInstant = " + nextTransitionInstant );
System.out.println( "nextTransactionZdt = " + nextTransactionZdt );
System.out.println( "isDstAfterTransition = " + isDstAfterTransition );

zone = Europe/Sofia

zdt: 2019-10-15T10:00+03:00[Europe/Sofia]

isDst: true

previousTransition = Transition[Gap at 2019-03-31T03:00+02:00 to +03:00]

nextTransition = Transition[Overlap at 2019-10-27T04:00+03:00 to +02:00]

nextTransitionInstant = 2019-10-27T01:00:00Z

nextTransactionZdt = 2019-10-27T03:00+02:00[Europe/Sofia]

isDstAfterTransition = false

我们可以看到 Europe/Sofia 中的下一个转换发生在 看起来像凌晨 4 点,而比 UTC 早 3 小时:

nextTransition = Transition[Overlap at 2019-10-27T04:00+03:00 to +02:00]

…但是由于我们正在做 Daylight Saving Time (DST)“后退”,我们将时钟的指针拨回到凌晨 3 点,比 UTC 早 2 小时:

nextTransactionZdt = 2019-10-27T03:00+02:00[Europe/Sofia]

我们可以看到 isDstAfterTransition 那一刻我们不再处于 DST。

请注意,在 27 日这一天,索非亚地区的人们如何经历凌晨 3-4 点两次。第一个 3-4 AM 时间比 UTC 早 3 小时。第二个 3-4 AM 时间比 UTC 早 2 小时。

而且,这意味着 27 日是 25 小时而不是 24 小时。