9

In Joda Time, one can easily use the DateTimeZone.isLocalDateTimeGap method to tell if a local date and time is invalid because it falls into the gap created by a spring-forward daylight saving time transition.

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

But how do you detect the fall-back transition? In other words, if a local date and time could be ambiguous because there is an overlap, how do you detect that? I would expect something like zone.isLocalDateTimeOverlap, but it doesn't exist. If it did, I would use it like so:

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

The Joda-Time documentation is clear that if there is an overlap during conversions, it will take the earlier possibility unless told otherwise. But it doesn't say how to detect that behavior.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575

1 Answers1

11

Take advantage of 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)); 
}
leonbloy
  • 73,180
  • 20
  • 142
  • 190