"how to handle dates when the input dates are in the transition period from pdt to pst?" Code Answer

1

neither simpledateformat nor the underlying calendar specifies what happens when parsing a datetime string without timezone for a time in the overlapping hour between daylight savings time and standard time.

you have observed that it will return the later time, i.e. it seems to prefer standard over daylight savings time. but, the behavior is undefined, so...

the new java.time classes do however specify exactly what happens, and how to choose the other "hour" of the overlap.

in the new api, since your datetime string is without timezone, you'd likely first parse using localdatetime, then apply time zone to get a zoneddatetime, e.g.

localdatetime ldtend = localdatetime.parse("2016-11-06t01:56:01");
zoneddatetime zdtend = ldtend.atzone(zoneid.of("america/los_angeles"));
// zdtend is now: 2016-11-06t01:56:01-07:00[america/los_angeles]

to see the overlap, you can try adding an hour:

zoneddatetime zdtend2 = zdtend.plushours(1);
// zdtend2 is now: 2016-11-06t01:56:01-08:00[america/los_angeles]

the behavior is well-defined, see javadoc of atzone():

in most cases, there is only one valid offset for a local date-time. in the case of an overlap, where clocks are set back, there are two valid offsets. this method uses the earlier offset typically corresponding to "summer".

in the case of a gap, where clocks jump forward, there is no valid offset. instead, the local date-time is adjusted to be later by the length of the gap. for a typical one hour daylight savings change, the local date-time will be moved one hour later into the offset typically corresponding to "summer".

to obtain the later offset during an overlap, call zoneddatetime.withlateroffsetatoverlap() on the result of this method. to throw an exception when there is a gap or overlap, use zoneddatetime.ofstrict(localdatetime, zoneoffset, zoneid).

as you can see, it will always return the earlier time in an overlap, which is opposite of the observed behavior of simpledateformat. if you want the later time in an overlap, call withlateroffsetatoverlap().

if you don't want to rely on documented default, you can always be explicit:

zoneid pt = zoneid.of("america/los_angeles");

localdatetime ldtstart = localdatetime.parse("2016-11-06t00:57:01");
zoneddatetime zdtstartearly = ldtstart.atzone(pt).withearlieroffsetatoverlap();
zoneddatetime zdtstartlater = ldtstart.atzone(pt).withlateroffsetatoverlap();
system.out.println(zdtstartearly); // 2016-11-06t00:57:01-07:00[america/los_angeles]
system.out.println(zdtstartlater); // 2016-11-06t00:57:01-07:00[america/los_angeles]

localdatetime ldtend = localdatetime.parse("2016-11-06t01:56:01");
zoneddatetime zdtendearly = ldtend.atzone(pt).withearlieroffsetatoverlap();
zoneddatetime zdtendlater = ldtend.atzone(pt).withlateroffsetatoverlap();
system.out.println(zdtendearly); // 2016-11-06t01:56:01-07:00[america/los_angeles]
system.out.println(zdtendlater); // 2016-11-06t01:56:01-08:00[america/los_angeles]

as you can see, for the 00:57 time, it makes no difference, because that time is not in the overlap hour.

By user1617707 on September 16 2022

Answers related to “how to handle dates when the input dates are in the transition period from pdt to pst?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.