java.text.ParseException : Unparseable date on some devices only 异常

标签 java android simpledateformat

我遇到了一个非常奇怪的问题。 解析此字符串 2016-09-06 05:18:06.023 PM 时出现以下异常 - java.text.ParseException: Unparseable date: "2016-09-06 05:18: 06.023 PM”(在偏移量 24 处)

奇怪的是,发生此异常的设备是 friend 的 Nexus 5。但是,如果我在我的 Nexus 5/其他几个模拟器 上调试相同的字符串,它工作正常。

这是我正在使用的代码。 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)

最佳答案

它可能会受到设备默认语言环境中上午/下午符号的影响,因此请尝试使用如下语言环境来解析日期,它会对您有所帮助。

   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();
      }

关于java.text.ParseException : Unparseable date on some devices only 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39361220/

相关文章:

java - android.database.sqlite.SQLiteException : near "UNION": syntax error

Android 搜索栏划分扇区

java - "Fri Jun 05 00:00:00 PDT 2015"的日期格式是什么

java - 无法将字符串解析为日期格式

java - 当日期格式为 dd-MMM-yyyy 时无法找到两个日期之间的天数

java - 是否可以在 Java 方法中声明扫描仪变量来读取键盘输入?

java - 删除按钮之前更改颜色

java - 如何修复 java.util.zip.ZipException : duplicate entry: com/google/firebase/FirebaseApiNotAvailableException. 类?

java - 将 JSON 数组从服务器 API 请求转换为 Retrofit2 中的对象

java - 找不到无法从 editText 读取的原因