I'm trying to convert this to a readable format however, keep getting java.text.ParseException: Unparseable date: "2016-11-18T11:13:43.838Z" (at offset 23)
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
Date date1 = df.parse("2016-11-18T11:13:43.838Z");
DateFormat outputFormatter1 = new SimpleDateFormat("dd-MMM-yyyy");
String output1 = outputFormatter1.format(date1); //
} catch (ParseException e) {
e.printStackTrace();
}
I read about adding locale as other SO answers suggested but it is still not working.
you are parsing a string that is not a correct representation of that pattern, you are missing the TimeZone... something like: -0600
example:
Date date1 = df.parse("2016-11-18T11:13:43.838-0600Z");
here is the doc for more info....
your code should look like:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
Date date1 = df.parse("2016-11-18T11:13:43.838-0600Z");
DateFormat outputFormatter1 = new SimpleDateFormat("dd-MMM-yyyy");
String output1 = outputFormatter1.format(date1); //
} catch (ParseException e) {
e.printStackTrace();
}
According to the docs the Z
in your format string indicates an RFC 822 time zone e.g. +01:00. You need to parse a ISO 8601 Time zone (the Z
in your input string indicating UTC timezone). You configure that with X
:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.US);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With