How to solve java.text.ParseException: Unparseable date?
Asked Answered
C

4

5

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.

Cryptogram answered 2/5, 2017 at 11:52 Comment(4)
Put the Z in single quotes in your SimpleDateFormat. Like this: DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");Tecumseh
you are missing the timezone....Caerphilly
@ΦXocę웃Пepeúpaツ He does have a timezone. Z is the zero timezone, equivalent to +0000.Komarek
Possible duplicate of ISO 8601 String to Date/Time object in AndroidKeown
F
6

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();
    }
Fellmonger answered 2/5, 2017 at 11:59 Comment(2)
This is the only thing that worked. The solution was to change how the back-end is saving date so I can format it on allowable patterns by SimpleDateFormat. Unless someone can provide a solution without changing the value of date returned by the api call, I think this is the best answer.Cryptogram
@Cryptogram -0600 is not the same as Z (=+0000). So if you change the input like that via string preprocessing you will get another result. If your JVM is too old (before Java-7 no support for X-pattern) then you should rather replace "Z" by "+0000" (or even "+00:00").Heterotaxis
B
6

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);
Bullish answered 2/5, 2017 at 11:59 Comment(1)
Hello, I tried X but getting "java.lang.IllegalArgumentException: Unknown pattern character 'X' at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:314)". BTW, I am using this in Android app and I just realize the pattern saved on our mongodb is not supported in android developer.android.com/reference/java/text/SimpleDateFormat.htmlCryptogram
F
6

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();
    }
Fellmonger answered 2/5, 2017 at 11:59 Comment(2)
This is the only thing that worked. The solution was to change how the back-end is saving date so I can format it on allowable patterns by SimpleDateFormat. Unless someone can provide a solution without changing the value of date returned by the api call, I think this is the best answer.Cryptogram
@Cryptogram -0600 is not the same as Z (=+0000). So if you change the input like that via string preprocessing you will get another result. If your JVM is too old (before Java-7 no support for X-pattern) then you should rather replace "Z" by "+0000" (or even "+00:00").Heterotaxis
E
2

Reading the Javadoc for SimpleDateFormat, I found that using Z for timezone is very strict. It will not recognize "Z" as zulu, it only accepts numeric timezone offsets. You might want to try X instead, which accept "Z" according to the documentation.

Enalda answered 2/5, 2017 at 12:1 Comment(0)
P
0

Please try this format

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Pollerd answered 14/1, 2019 at 9:24 Comment(2)
Incorrect answer. The Z in the string to parse is a UTC offset of 0. When you ignore it, you aren’t getting the correct time.Keown
Yes, correct solutions here and here.Keown

© 2022 - 2024 — McMap. All rights reserved.