Since your string uses English names for week day and month, you need to make sure that your SimpleDateFormat
uses a locale with English language, for example:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ROOT);
With this change your code prints (tested with Java 8):
Tue Apr 11 00:00:00 CEST 2017
EDIT: please fill in the appropriate (English-speaking) locale instead of Locale.ROOT
.
I agree with Ali Sağlam in warmly recommending the java.time
classes introduced in Java 8. You will still need to control the locale, though:
ZonedDateTime.parse("Tue Apr 11 00:00:00 CEST 2017",
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy",
Locale.ROOT))
.toLocalDateTime();
Finally, while we’re at the recommendations, see if you can find a way to avoid the three and four letter time zone abbreviations like CEST
. Many of them are ambiguous, and CEST isn’t even really a full time zone, just the summer half of the Central European time zone. While it works as expected with a date in the summer time half of year, I’m not even sure what I should expect if one day you have a date in winter with CEST
time zone abbreviation.