首页 > 解决方案 > new SimpleDateFormat("hh:mm a", Locale.getDefault()).parse("04:30 PM") giving Unparseable exception

问题描述

Strange things happen at most desperate times.

I'm experiencing a bit strange thing in this line of code

Date time = new SimpleDateFormat("hh:mm a", Locale.getDefault()).parse("04:30 PM");

is giving

"Unparseable date: \"04:30 PM\" (at offset 6)"

exception in Android 6.0 and Android 6.0.1 devices only, in production app (on Google Play Store).

PS: I'm unable to regenerate this bug on Android 6.0 Emulator & HTC Desire 10 Pro Android 6.0.1. Any help to regenerate this bug locally or how to way around it will be appreciated.

Edited: 15-Nov-2018 Devices giving this exception.

OS Version: 3.4.0-10662519 (G900FXXS1CQD8) OS API Level: 23 Device: klte Model (and Product): SM-G900F (kltexx) Manufacturer: samsung Other TAGS: release-keys SD Card state: mounted http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G900F Build/MMB29M)

OS Version: 3.10.84 (v1AJW-0) OS API Level: 23 Device: idol4 Model (and Product): 6055K (6055K) Manufacturer: TCL Other TAGS: release-keys SD Card state: mounted http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; 6055K Build/MMB29M)

OS Version: 3.10.84-g05b37ae (16293194481ff) OS API Level: 23 Device: p1 Model (and Product): LG-H818 (p1_global_com) Manufacturer: LGE Other TAGS: release-keys SD Card state: mounted http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0; LG-H818 Build/MRA58K)

标签: javaandroidtimesimpledateformatdatetime-parsing

解决方案


Offset 6 of your string is where it says PM.

It’s a locale issue. AM and PM, although derived from Latin are called that in English, not in very many other languages. Therefore those abbreviations are not recognized in very many locales. In your code you use Locale.getDefault(), and if it returns a non-English-speaking locale, you are likely to get the error. Try for example Locale.ENGLISH instead. Alternatively make sure you get a string in the right format and language for the default locale.

java.time

If you are doing any considerable work with times or dates in your app, and also for anyone programming for Java 8 or later or for Android API level 26 or higher: The classes you use, Date and SimpleDateFormat, have always had design problems, the latter in particular is typically troublesome. Fortunately both are long outdated now and replaced by java.time, the modern Java date and time API. So use this instead:

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
    LocalTime time = LocalTime.parse("04:30 PM", timeFormatter);
    System.out.println(time);

Output:

16:30

A LocalTime is a time of day without date and without time zone and seems to match your need much better than the old-fashioned Date class.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links


推荐阅读