1

I struggle with I guess trivial problem, but I don't see anything wrong here:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String strDate = "2014-12-07T13:35:08.030Z";
try
{
    return format.parse(strDate);
}
catch (ParseException e)
{
    e.printStackTrace();
    Log.e("Problem with formatting date", strDate);
}

I'm getting java.text.ParseException: Unparseable date: "2014-12-07T13:35:08.030Z" (at offset 19)

Any idea what is still wrong here?

poslinski.net
  • 201
  • 1
  • 3
  • 15

2 Answers2

3

According to the documentation it appears that Android-SimpleDateFormat does not support the Z-symbol (which is NOT a literal but a valid ISO-identifier and synonym for UTC+00:00).

So the pattern symbol Z only understands offsets like -0800, but not "Z". Just using apostrophs is not a sufficient workaround. You also need to set the timezone explicitly to UTC respective GMT+00:00 if you have your input ending with "Z". Something like that (not tested):

String input = "2014-12-07T13:35:08.030Z";
SimpleDateFormat format;

if (input.endsWith("Z")) {
  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  format.setTimeZone(TimeZone.getTimeZone("GMT"));
} else {
  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}

try {
    return format.parse(input);
} catch (ParseException e) {
    e.printStackTrace();
    Log.e("Problem with formatting date", input);
}
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
1

You just forget (') single quotation around Z in SimpleDateFormat :

'Z' instead of Z

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67