javaandroidparsingdateexception

Error by converting date: Unparseable date in JAVA


I can't find the problem. I'm trying to convert the date:

"Thu, 10 Jul 2014 13:33:26 +0200"

from string to Date with this code:

String formatType = "EEE, dd MMM yyyy HH:mm:ss Z";
Date startzeit = new SimpleDateFormat(formatType).parse(einsatz.getString("startzeit"));

but I'm getting this exceptoin:

java.text.ParseException: Unparseable date: "Thu, 10 Jul 2014 13:33:26 +0200"


Solution

  • You're creating a SimpleDateFormat without specifying a locale, so it'll use the default locale. By the looks of your variable names, that may not be English - so it'll have a hard time parsing "Thu" and "Jul".

    Try:

    String formatType = "EEE, dd MMM yyyy HH:mm:ss Z";
    Date startzeit = new SimpleDateFormat(formatType, Locale.US)
                            .parse(einsatz.getString("startzeit");
    

    (That works for me, with your sample value.)