java – Android Marshmallow中的SimpleDateFormat行为更改

前端之家收集整理的这篇文章主要介绍了java – Android Marshmallow中的SimpleDateFormat行为更改前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Android 6.0,Marshmallow上遇到了日期格式问题.抛出下面提到的异常的代码是我的应用程序用于API请求(“客户端”)的纯Java库(单独构建).该库是用Java 1.6构建的,如果它是相关的…无论如何,这是代码;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd E hh:mm aa",Locale.UK);
Date eventDate = dateFormat.parse(StringUtils.substring(record,23).trim());

……记录有价值;

2015-10-23 Fri 10:59 PM BST   3.60 meters

……“修剪”之后;

2015-10-23 Fri 10:59 PM
yyyy-MM-dd E hh:mm aa

这段代码自Froyo的古老时代以来一直有效,并经过单元测试.除了棉花糖之外,抛出异常;

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”是10:59中’9’之后的空格.谁能解释这个失败?

更新

切换到joda-time,它会发出更多信息错误消息.这里是;

Invalid format ... is malformed at "PM"

…所以,这是关于试图解析的字符串的AM / PM方面 – 回到the docs

解决方法

英国Locale似乎改变了“am”和“pm”的定义.

在Marshmallow,英国Locale现在“am”代表“a.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,25).trim());

我不能解释为什么,但美国地区与上午/下午和英国与上午/下午.

编辑

看来,作为2015年3月Locale更新的一部分,en_gb语言环境已替换其am / pm定义. source diff

原文链接:https://www.f2er.com/android/123844.html

猜你在找的Android相关文章