The input has weekday and month name in English, so it's probably a locale issue. SimpleDateFormat
uses the system's default locale if you don't specify one - check the value of java.util.Locale.getDefault()
in your system, probably it won't be en
(English).
If you set the locale, things should work:
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
formatter.parse("Mon Dec 14 13:25:00 CET 2015"));
New Java Date/Time API
The old classes (Date
, Calendar
and SimpleDateFormat
) have lots of problems and design issues, and they're being replaced by the new APIs.
If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
The code below works for both.
The only difference is the package names (in Java 8 is java.time
and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp
), but the classes and methods names are the same.
One big difference is that the new API uses IANA timezones names (always in the format Continent/City
, like America/Sao_Paulo
or Europe/Berlin
).
Avoid using the 3-letter abbreviations (like CET
or PST
) because they are ambiguous and not standard.
You can get a list of available timezones (and choose the one that fits best your system) by calling ZoneId.getAvailableZoneIds()
. In your case, CET
is a shortcut for "Central European Time", but there are more than 30 different regions (or timezones) that uses CET
abbreviation:
Europe/Ljubljana, Europe/Rome, Atlantic/Jan_Mayen, Europe/Berlin, Africa/Ceuta, Africa/Algiers, Europe/Zurich, Europe/Oslo, Europe/Amsterdam, Poland, Europe/Stockholm, Europe/Vatican, Europe/Budapest, Europe/Gibraltar, Europe/Bratislava, Europe/San_Marino, Europe/Madrid, Europe/Zagreb, Europe/Copenhagen, Europe/Malta, Europe/Brussels, Europe/Vienna, Europe/Busingen, Europe/Vaduz, Europe/Warsaw, Europe/Prague, Europe/Skopje, Europe/Podgorica, Europe/Paris, Africa/Tunis, Europe/Sarajevo, Europe/Tirane, Europe/Luxembourg, Europe/Andorra, Europe/Belgrade, Arctic/Longyearbyen, Europe/Monaco
I've chosen one for this test, but fell free to choose the one that suits best for your system.
// create formatter (this is thread-safe, while SimpleDateFormat isn't)
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// pattern for date/time
.appendPattern("EEE MMM dd HH:mm:ss ")
// timezone (use Europe/Berlin as prefered value for CET)
.appendZoneText(TextStyle.SHORT, Collections.singleton(ZoneId.of("Europe/Berlin")))
// year
.appendPattern(" yyyy")
// use English locale to parse weekday and month name
.toFormatter(Locale.ENGLISH);
// ZonedDateTime is a date and time with a timezone
ZonedDateTime z = ZonedDateTime.parse("Mon Dec 14 13:25:00 CET 2015", fmt);
System.out.println(z); // 2015-12-14T13:25+01:00[Europe/Berlin]
The output will be:
2015-12-14T13:25+01:00[Europe/Berlin]
If you want a custom format, you can use the formatter as well:
System.out.println(z.format(fmt)); // Mon Dec 14 13:25:00 CET 2015
The output will be:
Mon Dec 14 13:25:00 CET 2015