新的SimpleDateFormat("hh:mm a",Locale.getDefault()).parse("04:30 PM")给出了不可解析的异常 [英] new SimpleDateFormat("hh:mm a", Locale.getDefault()).parse("04:30 PM") giving Unparseable exception

查看:120
本文介绍了新的SimpleDateFormat("hh:mm a",Locale.getDefault()).parse("04:30 PM")给出了不可解析的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的事情经常在绝望的时候发生.

我在这段代码中遇到了一些奇怪的事情

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");

正在给予

无法解析的日期:\"04:30 PM \"(偏移量为6)"

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

仅在生产应用(在Google Play商店中)的Android 6.0和Android 6.0.1设备中例外.

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

PS:我无法在Android 6.0 Emulator&上重新生成此错误.HTC Desire 10 Pro Android 6.0.1.在本地重新生成此错误或如何解决该错误的任何帮助将不胜感激.

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.

2018年11月15日出现此异常的设备.

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

操作系统版本:3.4.0-10662519(G900FXXS1CQD8)操作系统API级别:23设备:klte型号(和产品):SM-G900F(kltexx)制造商:三星其他标签:释放键SD卡状态:已安装http.agent = Dalvik/2.1.0(Linux; U; Android 6.0.1; SM-G900F Build/MMB29M)

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)

操作系统版本:3.10.84(v1AJW-0)操作系统API级别:23设备:idol4型号(和产品):6055K(6055K)制造商:TCL其他标签:释放键SD卡状态:已安装http.agent = Dalvik/2.1.0(Linux; U; Android 6.0.1; 6055K 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)

操作系统版本:3.10.84-g05b37ae(16293194481ff)操作系统API级别:23设备:p1型号(和产品):LG-H818(p1_global_com)制造商:LGE其他标签:释放键SD卡状态:已安装http.agent = Dalvik/2.1.0(Linux; U; Android 6.0; LG-H818 Build/MRA58K)

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)

推荐答案

字符串的偏移量6表示 PM .

Offset 6 of your string is where it says PM.

这是一个地区问题. AM PM 尽管是从拉丁语衍生而来的,但它们用英语而不是其他许多语言来称呼.因此,这些缩写在很多地区都无法识别.在您的代码中,您使用 Locale.getDefault(),如果它返回的语言不是英语,则很可能会出错.尝试例如使用 Locale.ENGLISH .另外,请确保您以正确的格式和语言获取默认语言环境的字符串.

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 8或更高版本或Android API级别26或更高级别的编程,请使用以下类: Date SimpleDateFormat 一直存在设计问题,尤其是后者通常很麻烦.幸运的是,两者现在都已经过时了,现在已由现代Java日期和时间API 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);

输出:

16:30

LocalTime 是一天中没有日期和时区的时间,似乎比老式的 Date 类更好地满足了您的需求.

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.

是的,java.time在旧的和更新的Android设备上都能很好地工作.它只需要至少 Java 6 .

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

  • 在Java 8和更高版本以及更新的Android设备(API级别26起)中,内置了现代API.
  • 在Java 6和7中,获取ThreeTen反向端口,即新类的反向端口(JSR 310的ThreeTen;请参见底部的链接).
  • 在(较旧的)Android上,请使用Android版的ThreeTen Backport.称为ThreeTenABP.并确保使用子包从 org.threeten.bp 导入日期和时间类.
  • 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.
  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

这篇关于新的SimpleDateFormat("hh:mm a",Locale.getDefault()).parse("04:30 PM")给出了不可解析的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆