Determine future timezone transitions

后端 未结 1 700
孤独总比滥情好
孤独总比滥情好 2021-01-11 15:36

I need to predict when the next at least 2 timezone transitions will be for a particular timezone.

Java 8 offers the new java.time API, specifically

相关标签:
1条回答
  • 2021-01-11 16:01

    The method ZoneRules.getTransitions() doesn't list all transitions until infinity into the future (obviously). This gets the next two transitions:

    ZoneId zoneId = ZoneId.of("Australia/Sydney");
    ZoneRules rules = zoneId.getRules();
    
    ZoneOffsetTransition nextTransition = rules.nextTransition(Instant.now());
    System.out.println("Next transition at: " +
            nextTransition.getInstant().atZone(zoneId));
    
    ZoneOffsetTransition nextNextTransition =
            rules.nextTransition(nextTransition.getInstant());
    System.out.println("Next transition after that at: " +
            nextNextTransition.getInstant().atZone(zoneId));
    

    Output:

    Next transition at: 2016-10-02T03:00+11:00[Australia/Sydney]

    Next transition after that at: 2017-04-02T02:00+10:00[Australia/Sydney]

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