Search code examples
javadateexceptionunparseable

ParseDate Unparseable


I have Strings representing dates with the format 2014-11-01T18:57:24.497Z which I want to parse as SimpleDateFormat.

I am using the following code

// 2014-11-01T18:57:24.497Z
SimpleDateFormat startAnalyzing = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
Date start = startAnalyzing.parse(startDateAnalyzing);

When doing this I am getting an exception:

java.text.ParseException: Unparseable date: "2014-11-01T18:57:24.497Z"
    at java.text.DateFormat.parse(DateFormat.java:357)
    ...

What I am doing wrong?


Solution

  • Firstly you are trying to parse a z with Z so either choose z lower or upper for both (the String and the pattern).

    Secondly, you need to "escape" the Z in the pattern (or z).

    String startDateAnalyzing = "2014-11-01T18:57:24.497z";
    SimpleDateFormat startAnalyzing = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'z'");
    

    Output:

    Sat Nov 01 18:57:24 CET 2014