0

I am trying to parse a date in android using the following code

String dateString = "Sun Apr 15 13:37:33 CEST 2012";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println(sdf.parse(dateString));

The code above works fine with java, but in Android it gives ParseException.

java.text.ParseException: Unparseable date: "Sun Apr 15 13:37:33 CEST 2012" (at offset 20)

What could be the possible reason?

I am running it on Samsung S4, lollipop, system language is French, and system timezone is GMT+2:00.

  • check below answer @Naveed – Damini Mehra May 23 '16 at 08:41
  • 2
    Possible duplicate of [SimpleDateFormat.parse not working since lolipop 5.0 android update](http://stackoverflow.com/questions/29584235/simpledateformat-parse-not-working-since-lolipop-5-0-android-update) – Alex Zaitsev May 23 '16 at 08:49

1 Answers1

0

try this code:

    String mytime="Jan 17, 2012";

    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "MMM dd, yyyy");
    Date myDate = null;
    try {
        myDate = dateFormat.parse(mytime);

    } catch (ParseException e) {
        e.printStackTrace();
    }

    SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 

    zzz yyyy");
    String finalDate = timeFormat.format(myDate);
    TextView t = (TextView) findViewById(R.id.txt);
    t.setText(finalDate);
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
  • Thaks @damini for the response, I am working on an old project, and those string dates are coming from database, so I can not change them to something like String 'mytime="Jan 17, 2012";' – Naveed Iqbal May 23 '16 at 08:53
  • @NaveedIqbal it is just for sample string you can change it and change this format SimpleDateFormat dateFormat = new SimpleDateFormat( "MMM dd, yyyy"); accoring to your string – Damini Mehra May 23 '16 at 08:55