Date Serialization Fails for TimeZones on Daylight Savings TIme

Issue #423 resolved
Robert Robinson created an issue

The following code is incorrect SafeRepresenter.java+392 in basically all releases since at least 1.19:

int gmtOffset = calendar.getTimeZone().getOffset(calendar.get(Calendar.ERA),
                    calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.DAY_OF_WEEK),
                    calendar.get(Calendar.MILLISECOND));

Because calendar.get(Calendar.MILLISECOND) only returns the millisecond component of the time, not the number of milliseconds in the day, which is what is expected by the getOffset method.

Either calculate the total number of milliseconds in the day and use that value, or use:

int gmtOffset = calendar.getTimeZone().getOffset(calendar.getTime().getTime());

See: Calendar.getOffset

This only occurs on the day that daylight savings occurs, so tomorrow it'll work fine, but today, broken.

Comments (3)

  1. Log in to comment