某些具有相同API的设备上的Unparseable Date错误

时间:2017-08-21 14:47:42

标签: android date simpledateformat

我发现我正在开发的应用程序存在一个非常奇怪的问题。我正在从JSON对象读取数据以绘制图形,使用Genymotion Galaxy S6 5.1.0(API 22)时一切正常,但是当我在物理设备上尝试它时(华为P7和Moto) G 5.1.1)我得到了Unparseable Date错误,我的图显然是空白。

这是代码失败的代码:

SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd, HH:mm:ss");
try {
        formatter1.parse("Aug 01, 10:30:00");
    }catch(Exception e){e.printStackTrace();};

这就是我在logcat中的Stack Trace:

    08-21 16:37:37.200 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err: java.text.ParseException: Unparseable date: "Aug 01, 10:30:00" (at offset 0)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at java.text.DateFormat.parse(DateFormat.java:579)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at com.example.gonzalo.raspacuariofirebase.MainActivity.onCreate(MainActivity.java:58)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.app.Activity.performCreate(Activity.java:6078)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.app.ActivityThread.access$1200(ActivityThread.java:159)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.os.Looper.loop(Looper.java:135)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5569)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
08-21 16:37:37.210 16528-16528/com.example.gonzalo.raspacuariofirebase W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)

我知道另一个线程的问题与我的问题非常相似(java.text.ParseException: Unparseable date on some devices only),但事实是我没有使用AM / PM时间,所以我不知道我想我应该得到这个问题。

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要指定区域设置,否则可能会遇到这些错误:

https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

例如:

SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd, HH:mm:ss", Locale.US);

  

SimpleDateFormat类对语言环境敏感。如果你实例化   没有Locale参数的SimpleDateFormat,它将格式化日期   和时间根据默认的Locale。无论是模式还是模式   区域设置确定格式。对于相同的模式,SimpleDateFormat   如果区域设置不同,可能会以不同方式格式化日期和时间。