1

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?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Luixv
  • 8,590
  • 21
  • 84
  • 121

1 Answers1

3

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
user2336315
  • 15,697
  • 10
  • 46
  • 64