Determine future timezone transitions

杀马特。学长 韩版系。学妹 提交于 2019-11-30 23:35:19

问题


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 java.time.zone. ZoneRules.getTransitions() looks like exactly what I need however it doesn't list anything beyond the year 2010 for "Australia/Sydney".

What is the most reliable way to determine the next 2 timezone transition's date/time/offset?


回答1:


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]



来源:https://stackoverflow.com/questions/38736793/determine-future-timezone-transitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!