本文介紹了僅在某些設備上出現(xiàn)無法解析的日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我面臨著一個非常奇怪的問題。
解析此字符串2016-09-06 05:18:06.023 PM
時,出現(xiàn)以下異常-java.text.ParseException: Unparseable date: "2016-09-06 05:18:06.023 PM" (at offset 24)
奇怪的是,發(fā)生此異常的設備是朋友的Nexus 5。但是,如果我在我的Nexus 5/其他幾個仿真器上調(diào)試相同的字符串,它工作得很好。
以下是我正在使用的代碼。SimpleDateFormat
屬于java.text
包。Date
屬于java.util
包
SimpleDateFormat formatGMT = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss.SSS a");
formatGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
date = formatGMT.parse("2016-09-06 05:18:06.023 PM");
} catch (ParseException e) {
Crashlytics.log(Log.ERROR, "DB Insertion error", e.getMessage().toString());
Crashlytics.logException(e);
e.printStackTrace();
}
這是完整的堆棧跟蹤。
# Crashlytics - plaintext stacktrace Wed, 07 Sep 2016 03:37:44 GMT
# Platform: android
# Bundle Identifier: com.mypackage.app
# Issue #: 306
# Date: 2016-09-06T17:18:04Z
# OS Version: 6.0.1
# Device: Nexus 5
# RAM Free: 36.5%
# Disk Free: 11%
#0. Crashed: pool-3-thread-3: 0 0 0x0000000000000000
at java.text.DateFormat.parse(DateFormat.java:579)
at com.mypackage.app.MyService$16$1.execute(MyService.java:1670)
at io.realm.Realm$1.run(Realm.java:1187)
at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
--
Non-fatal Exception: java.text.ParseException: Unparseable date: "2016-09-06 05:18:06.023 PM" (at offset 24)
at java.text.DateFormat.parse(DateFormat.java:579)
at com.mypackage.MyService$16$1.execute(MyService.java:1670)
at io.realm.Realm$1.run(Realm.java:1187)
at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
#0. Crashed: pool-3-thread-3: 0 0 0x0000000000000000
at java.text.DateFormat.parse(DateFormat.java:579)
at com.mypackage.MyService$16$1.execute(MyService.java:1670)
at io.realm.Realm$1.run(Realm.java:1187)
at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
推薦答案
它可能會受到設備默認區(qū)域設置中的am/pm符號的影響,因此請嘗試使用下面的區(qū)域設置來解析它將對您有幫助的日期。
SimpleDateFormat formatGMT = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss.SSS a", Locale.US);
formatGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
try
{
date = formatGMT.parse("2016-09-06 05:18:06.023 PM");
}
catch (ParseException e)
{
Crashlytics.log(Log.ERROR, "DB Insertion error", e.getMessage().toString());
Crashlytics.logException(e);
e.printStackTrace();
}
這篇關于僅在某些設備上出現(xiàn)無法解析的日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,