gpt4 book ai didi

java - 如何检测 Joda Time 中不明确的 DST 重叠?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:32 29 4
gpt4 key购买 nike

Joda Time , 人们可以很容易地使用 DateTimeZone.isLocalDateTimeGap判断本地日期和时间是否无效的方法,因为它落入了 Spring 夏令时转换所造成的间隙。

DateTimeZone zone = DateTimeZone.forID("America/New_York");
LocalDateTime ldt = new LocalDateTime(2013, 3, 10, 2, 0);
boolean inGap = zone.isLocalDateTimeGap(ldt); // true

但是您如何检测回退转换呢?换句话说,如果本地日期和时间可能因为重叠而变得不明确,您如何检测呢?我希望像 zone.isLocalDateTimeOverlap 这样的东西,但它不存在。如果是这样,我会像这样使用它:

DateTimeZone zone = DateTimeZone.forID("America/New_York");
LocalDateTime ldt = new LocalDateTime(2013, 11, 3, 1, 0);
boolean overlaps = zone.isLocalDateTimeOverlap(ldt); // true

Joda-Time 文档清楚地表明,如果在转换过程中存在重叠,除非另有说明,否则它将采用较早的可能性。但它没有说明如何检测该行为。

最佳答案

利用withEarlierOffsetAtOverlap()

public static boolean isInOverlap(LocalDateTime ldt, DateTimeZone dtz) {
DateTime dt1 = ldt.toDateTime(dtz).withEarlierOffsetAtOverlap();
DateTime dt2 = dt1.withLaterOffsetAtOverlap();
return dt1.getMillis() != dt2.getMillis();
}


public static void test() {
// CET DST rolls back at 2011-10-30 2:59:59 (+02) to 2011-10-30 2:00:00 (+01)
final DateTimeZone dtz = DateTimeZone.forID("CET");
LocalDateTime ldt1 = new LocalDateTime(2011,10,30,1,50,0,0); // not in overlap
LocalDateTime ldt2 = new LocalDateTime(2011,10,30,2,50,0,0); // in overlap
System.out.println(ldt1 + " is in overlap? " + isInOverlap(ldt1, dtz));
System.out.println(ldt2 + " is in overlap? " + isInOverlap(ldt2, dtz));
}

关于java - 如何检测 Joda Time 中不明确的 DST 重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18793411/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com