Both of your format patterns are variations of the same, ISO 8601.
Easy in Joda-Time 2.3.
One line of code, using Joda-Time's built-in ISO 8601 formatter. That formatter handles both offsets, either zeros or a Z
.
org.joda.time.format.ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime( eitherStringGoesHere );
More detailed code…
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
String dateTimeStringZero = "2013-11-22T18:37:55.645+0000";
String dateTimeStringZulu = "2013-11-22T18:37:55.645Z";
org.joda.time.DateTime dateTimeZero = org.joda.time.format.ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime( dateTimeStringZero );
org.joda.time.DateTime dateTimeZulu = org.joda.time.format.ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime( dateTimeStringZulu );
Output…
System.out.println( "dateTimeZero: " + dateTimeZero );
System.out.println( "dateTimeZulu: " + dateTimeZulu );
When run…
dateTimeZero: 2013-11-22T18:37:55.645Z
dateTimeZulu: 2013-11-22T18:37:55.645Z
If you want a time zoned DateTime, change out the withZoneUTC()
. See the withZone
method. For user’s default time zone, simply omit any time zone call.