2016-08-03 6 views
10

次の少なくとも2つのタイムゾーン遷移がいつ特定のタイムゾーンになるかを予測する必要があります。将来のタイムゾーン遷移を決定する

Java 8には、新しいjava.time API、具体的にはjava.time.zoneが用意されています。 ZoneRules.getTransitions()は私が必要とするもののように見えますが、 "Australia/Sydney"のために2010年以降は何も表示されません。

次の2つのタイムゾーンの移行の日付/時刻/オフセットを決定する最も信頼性の高い方法は何ですか?

+0

あなたが綴ることができない@Thirler;) – pstanton

+0

申し訳ありませんが、この一般的にはそれ:) – Thirler

+3

を行うことができないため、ためのルール〜へ将来適用される政府によって変更することができます。彼らは時々予告して変更することがあります。次の*知っている*遷移を得ることができるはずです。 – Raedwald

答えて

12

メソッド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)); 

出力:

次に遷移時:2016-10-02T03:00 + 11:00 [オーストラリア/シドニー]

次の遷移後、この、次の2つの遷移を取得します時:2017-04-02T02:00 + 10:00 [オーストラリア/シドニー]

+2

はい、まだ驚いている 'getTransitions'は、少なくとも今日まで(つまりすべての過去)リストアップしていません。 – pstanton

+1

'getTransitions()'(または存在しない 'getFutureTransitions()')は、有益な無限の 'Stream'であった可能性のあるものを見たベストな例です。 – Thirler

+3

@Thirler無限の 'Stream'で' getFutureTransitions() 'を構築することは難しくありません:' Stream.iterate(rules.nextTransition(Instant.now())、t - > rules.nextTransition(t。 ' – Flown