Search code examples
javaandroidiso8601

Convert "2018-02-02T06:54:57.744Z" String to date in Android


Basically my date string is ISO-8601 date string, So I searched for converting ISO-8601 date string to date, but the solutions are lengthy. Here are the details.

I have a string 2018-02-02T06:54:57.744Z want to convert it to Date but I am getting error:

java.text.ParseException: Unparseable date: "2018-02-02T06:54:57.744Z"

I am using following technique to do that but no success:

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
        parser.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date parsed = parser.parse(utcDateString);
        return parsed;

I also use following patterns with SimpleDateFormat but no success:

yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd HH:mm:ss.S
yyyy-MM-dd'T'HH:mm:ssX
yyyy-MM-dd'T'HH:mm'Z'

any solution to this problem.


Solution

  • Z is timezone. You have to add timezone at the end of your date string or simply remove it from format: yyyy-MM-dd'T'HH:mm:ss

    A correct date string with timezone is something like this: 2018-02-02T06:54:57.744+0200