Search code examples
javaandroidparseexception

Unparseable date error


I have this

java.text.ParseException: Unparseable date: "Thu, 21 Apr 2016 18:00:00 +0000" (at offset 26)

when using new SimpleDateFormat("E, dd MMMM yyyy hh:mm:ss a", Locale.ROOT);

Why is that can be?

EDIT:

This is correct answer due to parsing pattern.

SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ROOT);

And also Android somehow gives error when the Locale is ROOT and when it is ENGLISH everything works fine.


Solution

  • You are using the wrong format. You should be using EEE, dd MMM yyyy HH:mm:ss Z instead of E, dd MMMM yyyy hh:mm:ss a.

    Here is the code snippet:

    public static void main (String[] args) throws Exception
    {
        String foo = "Thu, 21 Apr 2016 18:00:00 +0000";
        SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ROOT);
        System.out.println(f.parse(foo));
    }
    

    Output:

    Thu Apr 21 18:00:00 GMT 2016