First of all, I recommend you to stop using the old Date
and SimpleDateFormat
classes. They're outdated, full of bugs and design issues, and being replaced by the new date/time API.
If you're using Java 8, consider using the new java.time API.
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.
To parse the String
you want, just create a DateTimeFormatter
and set the java.util.Locale
to English, to make sure that weekdays and month names (in your case, Wed
and Jun
) are parsed correctly. If you don't set a locale, the system's default will be used (and if the default is not English, it won't work).
String date = "Wed Jun 21 14:14:23 GMT+08:00 2017";
// create formatter (using English locale to make sure weekdays and month names are parsed correctly)
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
// parse local date
LocalDate dt = LocalDate.parse(date, fmt);
System.out.println(dt.toString()); // 2017-06-21
The output is:
2017-06-21