Reputation: 1531
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"
Upvotes: 0
Views: 184
Reputation: 79055
java.time
In March 2014, Java 8 introduced the modern, java.time
date-time API which supplanted the error-prone legacy java.util
date-time API. Any new code should use the java.time
API.
You can parse your date-time string to an OffsetDateTime
using the inbuilt DateTimeFormatter#RFC_1123_DATE_TIME
.
Demo:
class Main {
public static void main(String args[]) {
OffsetDateTime odt = OffsetDateTime.parse("Thu, 10 Jul 2014 13:33:26 +0200",
DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println(odt);
}
}
Output:
2014-07-10T13:33:26+02:00
Note: If for some reason, you need an instance of java.util.Date
, let java.time
API do the heavy lifting of parsing your date-time string and convert odt
from the above code into a java.util.Date
instance using Date date = Date.from(odt.toInstant())
.
Learn more about the modern Date-Time API from Trail: Date Time
Upvotes: 1
Reputation: 1500395
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.)
Upvotes: 6