0

THis code is crashing in a few devices .

 SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a", Locale.ENGLISH);
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = null;
        try {
            date = format.parse(dtStart);
            System.out.println("Date ->" + date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long ere = date.getTime() / 1000;

Error:

System.err: java.text.ParseException: Unparseable date: "15-02-2016 08:56 p.m." (at offset 17)
System.err:     at java.text.DateFormat.parse(DateFormat.java:579)

3 Answers 3

2

You can use this workaround for your problem.

But actually Shlublu is correct.

private static void parseTime(String dtStart) {
    String time = dtStart.substring(0, dtStart.length() - 4);
    System.out.println(time);
    String timePick = dtStart.substring(dtStart.length() - 4, dtStart.length());
    System.out.println(timePick);
    if (timePick.equals("p.m.")) {
        time += "PM";
    } else if (timePick.equals("a.m.")) {
        time += "AM";
    }
    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a", Locale.ENGLISH);
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = null;
    try {
        date = format.parse(time);
        System.out.println("Date ->" + date);
    } catch (Exception e) {
        e.printStackTrace();
    }
    long ere = date.getTime() / 1000;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The issue comes from the a part of the pattern. It should be "PM", not "p.m.", according to the SimpleDateFormat documentation:

a   am/pm marker    (Text)  PM

This is consistent with the error message you received:

java.text.ParseException: Unparseable date: "15-02-2016 08:56 p.m." (at offset 17)

Offset 17 (zero-based) corresponds to the "p" of "p.m.".

Comments

0

The part of the date "p.m." is not recognized as an AM/PM specifier in SimpleDateFormat for the English locale. It should be AM or PM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.