BarmanInfo
BarmanInfo

Reputation: 392

Date format mismatch. Unparsable exception

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.

Upvotes: 0

Views: 233

Answers (1)

Anant Shah
Anant Shah

Reputation: 4044

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".

Upvotes: 2

Related Questions