2017-04-04 62 views
1

當夏令時結束時鐘返回1小時。這意味着將有2 01:30 AM上Novermber 5,2017年。因此,這裏是它如何工作如何檢測模糊的DST重疊?

01:59:58 
01:59:59 
01:00:00 
01:00:01 

正如你可以看到,當時間接近02:00:00它會回到1小時至01:00 :00。這意味着11月5日將會有一小時的重疊。我需要檢測給定的時間是否在重疊期內?

+0

您是否嘗試過與喬達時庫解析這些值?我知道,我遇到了一個特定的異常從庫與身邊的時候更改日期打交道時,所以它可能給一些背景上檢測它。我不確定內部Java版本的喬達時代如何處理這種情況。 – Jason

回答

1

如果你有興趣在transition rules那麼下面可以幫助你:

ZoneId yourZoneId = ... 
List<ZoneOffsetTransitionRule> transitionRules = yourZoneId.getRules() 
                  .getTransitionRules(); 

有了這些,你可以檢查你的特定時間是否在規則的定義範圍,或在下次發生轉換之內。

The ZoneDateTime javadoc還包含關於「缺口」和「重疊」和相應的偏移一些提示。

如果你只在你的時間是否是夏令時的時間或沒興趣,isDaylightSavings(Instant)可以幫助你:

ZoneId yourZoneId = ZoneId.of("Europe/Rome"); 
Predicate<ZonedDateTime> isDaylightSavings = 
          time -> yourZoneId.getRules() 
              .isDaylightSavings(time.toInstant()); 

boolean isDaylightSaving = isDaylightSavings.test(ZonedDateTime.of(/* the time to test */, yourZoneId)); 
// returns true if time is daylight savings time or false otherwise 

然而,這可能纔有意義,如果你的時間已經是ZonedDateTime

1

在你以前的SO-後my answer類似,要求區規則,你去:使用的

public static boolean isAmbivalent(LocalDateTime ldt, ZoneId zoneId) { 
    return zoneId.getRules().getValidOffsets(ldt).size() > 1; 
} 

例子:

LocalDateTime ldt = LocalDateTime.of(2017, 11, 5, 1, 30); 
ZoneId zoneId = ZoneId.of("America/Chicago"); 
System.out.println(isAmbivalent(ldt, zoneId)); // true, offsets either -5:00 or -6:00