在Android的棉花糖SimpleDateFormat的行为改变 [英] SimpleDateFormat behaviour change in Android Marshmallow
问题描述
我已经在Android 6.0,棉花糖遇到一个问题,日期格式。在code抛出异常下面说明的是一个纯Java库(单独建),其中我的应用程序使用API请求(以下简称客户)。该库是建立在Java 1.6,如果是相关的......反正这里是code;
I have encountered an issue with date formatting on Android 6.0, Marshmallow. The code throwing the exception noted below is a pure-Java library (built separately) which my application uses for API requests ("the client"). The library is built with Java 1.6 if that is related...anyway, here is the code;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd E hh:mm aa", Locale.UK);
Date eventDate = dateFormat.parse(StringUtils.substring(record, 0, 23).trim());
...的记录
具有价值;
2015-10-23 Fri 10:59 PM BST 3.60 meters
...其中微调后;
...which after "trimming" is;
2015-10-23 Fri 10:59 PM
yyyy-MM-dd E hh:mm aa
这code自升级Froyo的好时光工作,是单元测试。所有这一切抛开棉花糖抛出异常;
This code has worked since the good-old days of Froyo and is unit-tested. All that aside Marshmallow throws the exception;
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: SynchroniseTidePosition.doInBackground
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: java.text.ParseException: Unparseable date: "2015-10-23 Fri 10:59 PM" (at offset 21)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at java.text.DateFormat.parse(DateFormat.java:579)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at com.oceanlife.rover.handler.XTideParser.parseResponse(XTideParser.java:69)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at com.brantapps.oceanlife.task.SynchroniseTidePosition.doInBackground(SynchroniseTidePosition.java:107)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at com.brantapps.oceanlife.task.SynchroniseTidePosition.doInBackground(SynchroniseTidePosition.java:43)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at android.os.AsyncTask$2.call(AsyncTask.java:295)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
10-23 21:01:56.816 4091-4110/com.oceanlife E/ParseException: at java.lang.Thread.run(Thread.java:818)
偏移21是在十时59的'9'之后的空间中。任何人都可以解释这个故障?
Offset "21" is the space after the '9' in 10:59. Can anyone explain this failure?
更新的
切换到乔达时,它吐出一个更有意义的错误消息。这;
Switched over to joda-time and it spat out a more informative error message. Here it is;
Invalid format ... is malformed at "PM"
...等等,这是关于字符串试图解析的AM / PM方面 - 回的的文档
推荐答案
英国语言环境似乎有AM和PM改变。
The UK Locale appears to have had its definitions of "am" and "pm" altered.
在棉花糖英国语言环境现在有我psented为重$ P $是和PM,由时三十分。
In Marshmallow the UK Locale now has "am" represented as "a.m." and "pm" by "p.m."
String record = "2015-10-23 Fri 10:59 p.m. BST 3.60 meters"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd E hh:mm aa", Locale.UK);
// note the length is 25 now, not 23...
Date eventDate = dateFormat.parse(StringUtils.substring(record, 0, 25).trim());
我无法给予解释为什么,但在美国地区可与AM / PM和英国,AM / PM
I can't offer an explanation why, but the US Locale works with am/pm and the UK with a.m./p.m.
修改
看来,由于语言环境的更新在2015年三月的一部分, EN_GB
区域设置有其AM / PM的定义所取代。 <一href="https://android.googlesource.com/platform/external/icu/+/1b7d32f919554dda9c193b32188251337bc756f1%5E%21/icu4c/source/data/locales/en_GB.txt"相对=nofollow>来源差异
It appears that as part of the Locale updates in March 2015, the en_gb
locale had its am/pm definitions replaced. source diff
这篇关于在Android的棉花糖SimpleDateFormat的行为改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!