2

I want to convert : Wed Apr 06 09:37:00 GMT+03:00 2016 to 02/02/2012. What i tried

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    Date date = sdf.parse(mydate);

and

 String mydate =  "Wed Apr 06 09:37:00 GMT+03:00 2016";
            SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
            SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy");
            Date date = null;
            try {
                date = src.parse(mydate);
            } catch (ParseException e) {
                Log.d("deneme",e.getMessage());
            }
            String result = dest.format(date);

but its give error Unparseable date: "Wed Apr 06 09:37:00 GMT+03:00 2016" (at offset 0) any idea how i can parse it ?

1 Answers1

2

I think you are trying to format an english locale date but your system locale is not english. So when you create the SimpleDateFormat object specify the Locale explicity.

Try this code,

    String mydate = "Wed Apr 06 09:37:00 GMT+03:00 2016";
    SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
    SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
    Date date = null;
    try {
        date = src.parse(mydate);
    } catch (ParseException e) {
        Log.d("Exception",e.getMessage());
    }
    String result = dest.format(date);
    Log.d("result", result);
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
  • **For future visitors**: The legacy date-time API (`java.util` date-time types and their formatting type, `SimpleDateFormat`) is outdated and error-prone. It is recommended to stop using it completely and switch to `java.time`, the [modern date-time API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html). Using [this](https://stackoverflow.com/a/65544056/10819573) and [this](https://stackoverflow.com/a/67389571/10819573) answers, one should be able to solve it with `java.time` API. – Arvind Kumar Avinash May 09 '21 at 15:39