Android 某些具有相同API的设备上出现不可解析的日期错误
我发现我正在开发的应用程序有一个非常奇怪的问题。我从一个JSON对象读取数据来绘制一个图表,当使用Genymotion Galaxy S6 5.1.0(API 22)时,一切都正常,但当我在我的物理设备(华为P7和Moto G都是5.1.1)上尝试时,我得到了无法解释的日期错误,我的图表显然保持空白 以下是失败的代码:Android 某些具有相同API的设备上出现不可解析的日期错误,android,date,simpledateformat,Android,Date,Simpledateformat,我发现我正在开发的应用程序有一个非常奇怪的问题。我从一个JSON对象读取数据来绘制一个图表,当使用Genymotion Galaxy S6 5.1.0(API 22)时,一切都正常,但当我在我的物理设备(华为P7和Moto G都是5.1.1)上尝试时,我得到了无法解释的日期错误,我的图表显然保持空白 以下是失败的代码: SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd, HH:mm:ss"); try { for
SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd, HH:mm:ss");
try {
formatter1.parse("Aug 01, 10:30:00");
}catch(Exception e){e.printStackTrace();};
这是我在logcat中得到的堆栈跟踪:
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)
我知道有另一个线程的问题与我的()非常相似,但事实是我没有使用am/PM时间,所以我认为我不应该遇到这个问题
提前感谢您需要指定区域设置,否则可能会遇到以下错误: 例如:
SimpleDateFormat formatter 1=新的SimpleDateFormat(“mmmdd,HH:mm:ss”,Locale.US)代码>
SimpleDataFormat类对区域设置敏感。如果你实例化
SimpleDataFormat没有区域设置参数,它将格式化日期
和时间,根据默认区域设置。模式和方法
语言环境决定了格式。对于相同的模式,请使用SimpleDataFormat
如果区域设置不同,则日期和时间的格式可能不同
您将希望包括年份以避免此错误您没有使用AM/PM,但您使用的是英文月份名称(Aug),两者都与相同的原因有关:它们是区域敏感数据。您应该使用Locale.US
或Locale.ENGLISH
,正如在另一个细节中建议的那样,输入没有年份,SimpleDateFormat
将获得它的默认值(在我的机器中,它设置为1970
)。根据您对解析日期所做的操作,这可能会导致不正确的结果。@petey,我相信SimpleDataFormat
会很高兴地给您一个默认年份1970。这并不是你通常想要的,但我怀疑它会因为这个原因抛出任何异常。