Details
-
Type: Bug
-
Status: Closed
-
Priority: Major
-
Resolution: Not A Bug
-
Affects Version/s: 1.3.1
-
Fix Version/s: None
-
Component/s: Converters
-
Labels:None
-
JDK version and platform:JRE 6
Description
There are two problems with this converter (sorry I'm not submitting a patch, I don't have the source checked out from your repository)
1) The UTC parsers should be created with .withOffsetParsed() otherwise they will ignore the specified timezone information.
Like this:
private static final DateTimeFormatter[] formattersUTC = new DateTimeFormatter[]{ ISODateTimeFormat.dateTime().withOffsetParsed(), ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed(), ISODateTimeFormat.basicDateTime().withOffsetParsed(), ISODateTimeFormat.basicOrdinalDateTime().withOffsetParsed(), ISODateTimeFormat.basicOrdinalDateTimeNoMillis().withOffsetParsed(), ISODateTimeFormat.basicTime().withOffsetParsed(), ISODateTimeFormat.basicTimeNoMillis().withOffsetParsed(), ISODateTimeFormat.basicTTime().withOffsetParsed(), ISODateTimeFormat.basicTTimeNoMillis().withOffsetParsed(), ISODateTimeFormat.basicWeekDateTime().withOffsetParsed(), ISODateTimeFormat.basicWeekDateTimeNoMillis().withOffsetParsed(), ISODateTimeFormat.ordinalDateTime().withOffsetParsed(), ISODateTimeFormat.ordinalDateTimeNoMillis().withOffsetParsed(), ISODateTimeFormat.time().withOffsetParsed(), ISODateTimeFormat.timeNoMillis().withOffsetParsed(), ISODateTimeFormat.tTime().withOffsetParsed(), ISODateTimeFormat.tTimeNoMillis().withOffsetParsed(), ISODateTimeFormat.weekDateTime().withOffsetParsed(), ISODateTimeFormat.weekDateTimeNoMillis().withOffsetParsed(),};
2) When the UTC parsers are used, the timezone information in the resulting Calendar is still overwritten with the local timezone. Joda will take care of providing the local timezone by default if no timezone is explicitly specified, so this is all that's needed here:
for (int i = 0; i < formattersUTC.length; i++) { DateTimeFormatter formatter = formattersUTC[i]; try { DateTime dt = formatter.parseDateTime(str); Calendar calendar = dt.toGregorianCalendar(); return calendar; } catch (IllegalArgumentException e) { // try with next formatter } }
Test case (assumes that you're not actually in CST).