Parsing Iso 8601 Date Format Like 2015-06-27T13:16:37.363Z in Java

Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java

Try with this pattern (note the X at the end and the 'T' in the middle):

"yyyy-MM-dd'T'HH:mm:ss.SSSX"

From Java's SimpleDateFormat's documentation:

ISO 8601 Time zone:

...

For parsing, "Z" is parsed as the UTC time zone designator.

And, from the part where it describes the different characters:

X - Time zone - ISO 8601 time zone

EDIT

If using Android, then "X" is not supported.

You can use this pattern (note Z is a literal now):

"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

But then you'll get the date on your current timezone and would need to convert it to UTC if needed.

Setting current date from ISO 8601 in object Calendar format using Java

To create a Calendar from a String, you can use a SimpleDateFormat (as already suggested by the other answers). The formatter will parse the String and create a Date object, which will be set to the Calendar:

String tradeDate = "2017-06-01T15:49:18Z";

// create Calendar
Calendar cal = Calendar.getInstance();
// create formatter
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
// parse string and set the resulting date to Calendar
cal.setTime(df.parse(tradeDate));

The Calendar will contain the date equivalent to 2017-06-01T15:49:18Z. Note that this date/time is in UTC - the Z in the end is the UTC designator.

But if you try to print this date (using System.out.println(cal.getTime()), a logger, or even checking its value in a debugger), it'll implicity use the toString() method, and this converts the fields (day, month, year, hour, minute, seconds, etc) to the system's default timezone (if your default timezone is in India, for example, the date will be printed as Thu Jun 01 21:19:18 IST 2017, although the internal value won't be changed). But the value will still be equivalent to the UTC input.

Don't be misleaded by the output of toString() method from Calendar and Date classes. What matters is the value of the timestamp: the number of milliseconds since 1970-01-01T00:00Z, which can be checked with cal.getTimeInMillis(). Check this article for more information.


As you're using Java 7, there's another (better) alternative: you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes.

You can parse the input to a org.threeten.bp.ZonedDateTime and use the org.threeten.bp.DateTimeUtils class to convert it to a java.util.Calendar:

String tradeDate = "2017-06-01T15:49:18Z";
// parse input
ZonedDateTime zdt = ZonedDateTime.parse(tradeDate);
// convert to calendar
Calendar cal = DateTimeUtils.toGregorianCalendar(zdt);

Using this backport eliminates lots of problems and design issues of the old Calendar API. And makes a future migration to Java 8 much easier, as in new Java 8 API the classes and methods names are the same, just the packages are different (in Java 8 is java.time and in ThreeTen Backport is org.threeten.bp).

How can I parse this strange date-time format with Joda into a UTC DateTime?

Ignoring the microseconds issue pointed out by Meno Hochschild, use the parser to parse as normal, then call upon DateTime.withZone(DateTimeZone newZone) on the result of that to return a new instance of the DateTime converted to the desired time zone.

parse String to 'yyyy-mm-ddThh:mm:ss.SSSZ' ISODate java

Your code is not working since Z is a reserved character used to interpret RFC-822 time zones :

RFC 822 time zone: For formatting, the RFC 822 4-digit time zone format is used:

 RFC822TimeZone:
Sign TwoDigitHours Minutes
TwoDigitHours:
Digit Digit

Since Java 7, you can use X to interpret ISO-8601 time zones https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html . The following works :

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");

However, on my computer,

System.out.println(newDateString);

results in the following output :

2017-02-17T05:23:17.452+01

Alternatively, if you are sure to have only UTC dates, you could escape the Z letter with simple quotes :

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

And here is the displayed result :

2017-02-17T04:23:17.452Z



Related Topics



Leave a reply



Submit