1

I am trying to parse April 18 2016 10:41 AM to 04/18/2016 10:41. Here is my code

private String dateFormat(String strCurrentDate) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("mmmm dd yyyy hh:mm Z");
        Date newDate = null;
        newDate = format.parse(strCurrentDate);
        format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
        String date = format.format(newDate);
        Log.d("DATE_FORMATE_TESTING", date);
        return date;

    }

But it gives the following errors

04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: java.text.ParseException: Unparseable date: "April 18 2016 01:41 PM" (at offset 0)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:     at java.text.DateFormat.parse(DateFormat.java:579)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:     at gps.clock.com.MainActivity.dateFormat(MainActivity.java:441)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:     at gps.clock.com.MainActivity.access$500(MainActivity.java:70)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err:     at gps.clock.com.MainActivity$1$4.onClick(MainActivity.java:303)

Can anyone tell me how should I fix it? Why it is showing Unparseable date. How should I parse it? Thanks in advance.

anuradha
  • 692
  • 1
  • 9
  • 22
  • are you using Marshmallow? – Janki Gadhiya Apr 18 '16 at 04:52
  • 1
    You have lowercase `m`s for the month in the first pattern. They should be uppercase. And the character for am/pm is `a`, not `Z`. Please refer to the [`SimpleDateFormat` docs](http://developer.android.com/reference/java/text/SimpleDateFormat.html) – Mike M. Apr 18 '16 at 04:52
  • 3
    Possible duplicate of [incorrect conversion of String into date format](http://stackoverflow.com/questions/13698165/incorrect-conversion-of-string-into-date-format) – Dawood ibn Kareem Apr 18 '16 at 04:54
  • @MikeM. I tried with MMMM instead of mmmm. But still I a geting the same error. – anuradha Apr 18 '16 at 04:58
  • Did you change the `Z` as well? – Mike M. Apr 18 '16 at 05:03
  • @MikeM. no I didn't changed it. But it worked for me when I replaced Z with a. Can you please tell me what does it mean? – anuradha Apr 18 '16 at 06:48
  • The `Z` character represents the time zone, which your date `String` doesn't have. It has am/pm, though, which is represented by the `a` character. Refer to the [`SimpleDateFormat` docs](http://developer.android.com/reference/java/text/SimpleDateFormat.html) for the available pattern characters and their meanings. – Mike M. Apr 18 '16 at 06:53

3 Answers3

3

Try the following code

    String strCurrentDate = "April 18 2016 10:41 AM";
    SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a");
    Date newDate = null;
    try {
        newDate = format.parse(strCurrentDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
    String date = format.format(newDate);
    System.out.print(date);
user1799171
  • 567
  • 1
  • 3
  • 15
  • yes this worked for me. I will accept this answer. Can you please tell me what is the difference between a and Z that is in the SimpleDateFormate("MMM dd yyyy hh:mm a")? – anuradha Apr 18 '16 at 06:47
  • 'a' is used to represent AM or PM, where as 'Z' (Zulu time)represents TimeZone, So it wasn't able to parse. – user1799171 Apr 18 '16 at 07:23
1

I think you need to tweak your original format string.

SimpleDateFormat format = new SimpleDateFormat("mmmm dd yyyy hh:mm Z");

Should be

SimpleDateFormat format = new SimpleDateFormat("MMMM dd yyyy hh:mm a");

Jesse Buss
  • 833
  • 6
  • 13
0

In java date formatting m and M both have different meaning. m is used to parse minutes while M is used to parse month.

In your example you are parsing month with lower case m and that causes error. As suggested by @Mike M. change your ms with upper case.

Apurva
  • 7,871
  • 7
  • 40
  • 59