0

我必须阅读以下格式的日期Sat, 19 Jan 2013 00:00:00 +0100
我的格式化程序是

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");

当我想设置数据时:

try {
    myObject.setEndDate(format.parse(currentValue));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

如果我在简单的 java 项目或我的NexusHtc上使用它,这非常有效,但是当我将调试器附加到三星 s3时,它会给出解析器异常。

我希望这适用于所有版本的 android 和手机。

4

3 回答 3

0
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSSZ");

试试这个:

于 2013-03-15T13:25:19.280 回答
0

在 catch 中打印currentValue, 和Locale.getDefaultLocale()。语言环境很可能会阻止解析 currentValue。

至少你可以在具有完全相同参数的 PC 上尝试代码。

于 2013-03-15T15:04:19.693 回答
0

我通过在 try catch 中计算没有简单日期格式的日期来解决这个问题 当在某些手机上运行时,它没有进入 catch 并且格式运行良好,并且在其他手机上运行它进入 catch 但日期计算得很好。

   if(localName.equals("startDate") && currentValue!=null){

    try {
        myObject.setStartDate(format.parse(currentValue));
    } catch (ParseException e) {
        String  dates [] = currentValue.split(" ");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dates[1]));
        cal.set(Calendar.YEAR, Integer.parseInt(dates[3]));
        cal.set(Calendar.MONTH, StaticDataUtils.months.get(dates[2]));
        String hours [] = dates[4].split(":");
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hours[0]));
        cal.set(Calendar.MINUTE, Integer.parseInt(hours[1]));
        myObject.setStartDate(cal.getTime());
        e.printStackTrace();
    }
于 2013-03-18T10:50:55.717 回答