-2

The date is Wed Jun 21 14:14:23 GMT+08:00 2017 and I want to convert it into 2017-06-21.

Here is my code:

String date = "Wed Jun 21 14:14:23 GMT+08:00 2017";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMMM dd HH:mm:ss 'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat outputDate = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
    d = sdf.parse(date);
} catch (ParseException e) {
    e.printStackTrace();
}
Manpreet Singh
  • 1,048
  • 7
  • 7

3 Answers3

1

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

0

Try this works perfect

String  formatDate = "Wed Jun 21 14:14:23 GMT+08:00 2017";
        java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" , Locale.ENGLISH);
        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);

        Date date = null;
        try {
            date = dateTimeFormat.parse(formatDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String format = dateFormat.format(date);

        Log.d("===========>>>>>",format);
Anil
  • 1,605
  • 1
  • 14
  • 24
0

Try this and it works as you want.

String input_date = "Wed Jun 21 14:14:23 GMT+08:00 2017";
SimpleDateFormat fromthis = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
SimpleDateFormat tothis = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = null;
try {
       date = fromthis.parse(input_date);
    } catch (ParseException e) {
       e.printStackTrace();
    }
String result = tothis.format(date);
System.out.println(result);

Input: Wed Jun 21 14:14:23 GMT+08:00 2017

Output: 2017-06-21

Thank you.

Rameshbabu
  • 611
  • 2
  • 7
  • 21