How to solve java.text.ParseException: Unmatched date?
I am trying to convert this to readable format, but 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've read about adding locale like other SO answers, but it still doesn't work.
source to share
you are parsing a string that is not a correct representation of this pattern, you are missing a TimeZone ... something like: -0600
Example:
Date date1 = df.parse("2016-11-18T11:13:43.838-0600Z");
here's the doc for more information ....
your code should look like this:
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();
}
source to share
As per the docs Z
, your format string specifies the RFC 822 timezone for example. +01: 00. You need to parse the ISO 8601 time zone ( Z
in the input line indicating the UTC time zone). You configure this with X
:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.US);
source to share
Reading the Javadoc for SimpleDateFormat , I found the use Z
for timezone is very strict, It does not recognize "Z"
as zulu, it only accepts numeric timezone offsets. Instead, you can try X
which is accepted "Z"
according to the documentation.
source to share