Time parsing issue on Android

Viewed 1010

I am getting a parse exception when trying to parse the time string 02:22 p.m..

I have the following conversion function:

public static long convertdatetotimestamp(String datestring, String newdateformat, String olddateformat){
    SimpleDateFormat originalFormat = new SimpleDateFormat(olddateformat,Locale.ROOT);
    SimpleDateFormat targetFormat = new SimpleDateFormat(newdateformat,Locale.ROOT);
    Date date = null;
    try {
        date = originalFormat.parse(datestring);
        String formattedDate = targetFormat.format(date);
        Date parsedDate = targetFormat.parse(formattedDate);
        long nowMilliseconds = parsedDate.getTime();

        return nowMilliseconds;
    } catch (ParseException e) {
        e.printStackTrace();
        return 0;
    }

}

The method is called in another activity with a time format "02:22 p.m.". olddateformat and newdateformat are the same: hh:mm a.

It causes following error in log:

java.text.ParseException: Unparseable date: "02:22 p.m." (at offset 6)

How to resolve this issue? Time is in exactly above mentioned format.

3 Answers
Related