SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String dtc = "2014-03-27T11:31:42.798Z";
Date date = null;
try {
date = readDate.parse(dtc);
} catch (ParseException e) {
Log.d("myLog", "dateExcep " + e);
}
try{} catch{} have exception: 03-27 16:29:48.459: D/myLog(19388): dateExcep java.text.ParseException: Unparseable date: "2014-03-27T11:31:42.798Z" (at offset 23)
The Z
in your format pattern is the problem. That represents an RFC 822 time zone, which can't just be Z
. If your input will always be in UTC, you can use:
// The Z is now quoted as a literal.
SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
readDate.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
Note that it's important to set the time zone here, as otherwise it will assume the value is in your current system time zone.
In "normal" Java 7 you could use X
instead to represent an ISO-8601 time zone offset specifier, but that isn't supported in the Android SimpleDateTime
right now.
See more on this question at Stackoverflow