0

I want to change

"01-Jun-2016 07:54 AM"

to

"26th May,2016"

Here is my code

DateFormat df = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
DateFormat existinFormat= new SimpleDateFormat("dd-MMM-yyyy HH:mm aa " , Locale
            .ENGLISH);
Calendar c = Calendar.getInstance();
String newDateString = "";
Date startDate = null;
try {
    startDate = existinFormat.parse((date));
    newDateString = df.format(startDate);
} catch (ParseException e) {
    e.printStackTrace();
}

but its throws parse exception :

java.text.ParseException: Unparseable date: "01-Jun-2016 07:54 AM" (at offset 20)

If any one found this solution please share with me.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
BarmanInfo
  • 392
  • 1
  • 18

1 Answers1

2

You can do like this:

String dateStr = "01-Jun-2016 07:54 AM";
DateFormat existingformat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss aa");
DateFormat newformat = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
Date date = null;
try {
    date = existingformat.parse(dateStr);
} catch (ParseException e) {
    e.printStackTrace();
}

if (date != null) {
    String resultFormattedDate = newformat.format(date);
    Log.d("resultFormattedDate"," is:"+resultFormattedDate);
}

and can you please explain which type of date format is needed to you? I explained here by reference of your code with resultDateFromat is like "MMM dd, yyyy".

Anant Shah
  • 3,744
  • 1
  • 35
  • 48
  • in existingformat dateFormat change a instead of aa. And its working.. Thank you.. – BarmanInfo Jun 01 '16 at 09:36
  • @BarmanInfo Your question "Date format mismatch. Unparsable exception" is solved now. So you can accept my answer and by enhance your question you can achieve "1st Jun, 2016" by using [this reference link](http://stackoverflow.com/a/33540720/5059725) . This link will solve your problem. – Anant Shah Jun 01 '16 at 10:52