In a previous post I complained that Java Date Format was not symmetric.
Well, turns out Java 7’s Joda Time is also not necessarily symmetric… even when the formatter is not lossy!
Try it and see (for this to work, your current system time must not be UTC!)
DateTime now = new DateTime(DateTimeZone.UTC).withMillis(0);
String nowText = ISODateTimeFormat.dateTimeNoMillis().print(now);
DateTime then = ISODateTimeFormat.dateTimeNoMillis().parse(nowText);
and your test will fail! Turns out that the reparsing of the date loses the TimeZone information, so that even though the printed format of the nowText string is correct, the reparsing doesn’t initialize then with the timezone in the response. This, according to Joda, this is considered buggy but won’t be changed for historical reasons (sounds like any other Java Date/Calendar packages?) but it would be useful if this was clearly stated, up front in the documentation. Calling
DateTime then = DateTime.parse(nowText);
will give the correct result… which looks obvious when written here, but is not necessarily so obvious when you’re deep in your debugger. .
So, another new DateTimeCalendar package for Java 9 anyone? 🙂