我需要预测下一个至少2个时区过渡将是一个特定的时区.
Java 8提供了新的java.time API,特别是java.time.zone. ZoneRules.getTransitions()看起来像我所需要的,但是它没有列出2010年以后的“澳大利亚/悉尼”.
什么是最可靠的方式来确定下一个2时区转换的日期/时间/偏移量?
解决方法
方法ZoneRules.getTransitions()不会列出所有转换,直到将来的无穷远(显然).这将获得接下来的两个转换:
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));
输出:
Next transition at: 2016-10-02T03:00+11:00[Australia/Sydney]
Next transition after that at: 2017-04-02T02:00+10:00[Australia/Sydney]