簡體   English   中英

Android Marshmallow中的SimpleDateFormat行為更改

[英]SimpleDateFormat behaviour change in Android Marshmallow

我在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, 0, 23).trim());

...... record有價值;

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方面 - 返回到文檔

英國Locale似乎改變了“am”和“pm”的定義。

在Marshmallow,英國Locale現在“am”代表“am”,“pm”代表“pm”

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和英國上午/下午合作

編輯

看來,作為2015年3月Locale更新的一部分, en_gb語言環境已替換其am / pm定義。 源差異

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM