0

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"

fahu
  • 1,511
  • 4
  • 17
  • 26
  • 1
    Whats your default locale?At times due to the locale the string isn't parsed properly.Try this Date startzeit = new SimpleDateFormat(formatType,Locale.ENGLISH).parse(einsatz.getString("startzeit")); – humblerookie Jul 10 '14 at 12:54
  • side note: you IDE should give you a warning for using SimpleDateFormat without a locale. – njzk2 Jul 10 '14 at 13:15
  • [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash Jun 25 '21 at 22:16

1 Answers1

6

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194