how to parse date using SimpleDateFormat in java of type 2015 01 13T10:24:55Z

i have the following date string coming from grails services in android app.

2015-01-13T10:24:55Z

Now i want to parse this string using SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); but i am getting the error following error in logcat.

01-13 10:53:50.674: W/System.err(1794): java.text.ParseException: Unparseable date: "2015-01-13T10:24:55Z" (at offset 10)

please let me know about how to parse this type of string.

Jon Skeet
people
quotationmark

Currently you're using the "general time zone" specifier. In order to have Z interpreted as "this value is in UTC", you should use X:

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US)

X is an ISO-8601 time zone specifier, which includes Z for UTC.

Alternatively, you could quote it as a literal and explicitly specify the time zone as UTC:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
                                               Locale.US);
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

It's important to explicitly set the time zone in this case, as otherwise the value will be parsed using the system default time zone.

people

See more on this question at Stackoverflow