2016-10-18 6 views
1

ユーザーからの入力に基づいてList<Interval> intervalsを返す必要があります。範囲に夏時間が含まれていない限り、すべて正常に動作します。それが起こった場合、私が得た応答は、その特定の日(この例では11月1日のESTタイムゾーン)のあとにちょっと混乱してしまいます。UTC(joda間隔)からの異なるタイムゾーンに基づくJava夏時間の処理

私のデータはDBにUTCとして保存されているので、私はUTCで作業しています(リクエストはUTCで、私の応答はUTCでもあります)。しかしブラウザでは時刻はローカルのタイムゾーンに変換されます。バックエンドで私はユーザーのタイムゾーンにアクセスできます。
質問は、サマータイムを含むUTCへの応答時間を変換する方法です。ここ

public List<Interval> buildIntervals(BinInterval binSize) { 
    final DateTime intervalStart = binSize.getInterval().getStart(); 
    final DateTime intervalEnd = binSize.getInterval().getEnd(); 

    final DateTimeZone tz = getCurrentUserTimezone(); 
    List<Interval> intervals = new ArrayList<>(); 

    MutableDateTime currentBinStart = new MutableDateTime(intervalStart); 
    MutableDateTime currentBinEnd = new MutableDateTime(intervalStart.plus(binSize.getPeriod())); 

    while (currentBinEnd.isBefore(intervalEnd)) { 

    intervals.add(makeInterval(currentBinStart, currentBinEnd)); 

     currentBinStart = new MutableDateTime(currentBinStart.toDateTime().plus(binSize.getPeriod())); 
     currentBinEnd = new MutableDateTime(currentBinEnd.toDateTime().plus(binSize.getPeriod())); 

    } 
} 

private static Interval makeInterval(BaseDateTime start, BaseDateTime end) { 
    return new Interval(start.toDateTime().withZone(DateTimeZone.UTC), 
         end.toDateTime().withZone(DateTimeZone.UTC)); 
} 

間違っている試料の応答である:

行17の正しいバージョンがあるべきである。行17からendDate: "2015-11-02T05:00:00.000z"

enter image description here

及び終了時間をさらに+5であるべきです。
18行目以降では、開始時間も+5にする必要がありますが、何らかの理由で、日照時間の前後で正しい時間に変換されません。

11月1日以降に範囲を選択すると、それが完全に機能し、+ 5に変換されます。

私のローカルタイムゾーンはESTです。

答えて

1

私は、夏時間が起こっている特別なケースのために、異なるサイズの間隔を作成する必要があると仮定します。

開始時刻と終了時刻のオフセットを取得し、値に基づいて終了日をシフトする必要があるかどうかを判断することをお勧めします。

public static void main(String[] args) { 
    DateTimeZone EDT = DateTimeZone.forID("America/Toronto"); 
    DateTime start = new DateTime(2016, 5, 15, 4, 0, DateTimeZone.UTC); 
    DateTime end = start.plusDays(2); 

    int offset1 = (int) TimeUnit.MILLISECONDS.toMinutes(EDT.getOffset(start.getMillis())); 
    int offset2 = (int) TimeUnit.MILLISECONDS.toMinutes(EDT.getOffset(end.getMillis())); 
    if (offset1 != offset2) { 
     end = end.plusMinutes(offset1 - offset2); 
    } 

    System.out.println(new Interval(start.toDateTime().withZone(EDT), 
      end.toDateTime().withZone(EDT))); 

} 
関連する問題