简体   繁体   English

SimpleDateFormat在某些Android版本上不起作用

[英]SimpleDateFormat does not work on some android versions

I have to read dates in following format Sat, 19 Jan 2013 00:00:00 +0100 . 我必须阅读以下格式的日期, 星期六,2013年1月19日00:00:00 +0100
My formatter is 我的格式化程序是

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

When I want to set data : 当我想设置数据时:

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

This works perfect if I use it in simple java project or on my Nexus and Htc but when I attach to debugger a Samsung s3 it gives parser exception . 如果我在简单的Java项目中或在NexusHtc上使用它,则此方法非常完美,但是当我将调试器附加到Samsung s3时,它会给出解析器异常。

I want this to work on all versions of android and phones. 我希望这适用于所有版本的android和手机。

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

尝试这个:

In the catch print the currentValue , and Locale.getDefaultLocale() too. 在捕获中打印currentValueLocale.getDefaultLocale() Most likely the locale prevents parsing of the currentValue. 语言环境最有可能阻止解析currentValue。

At least then you might try the code on a PC with exact same parameters. 至少您可以在具有完全相同参数的PC上尝试使用该代码。

I solved this by computing date without simple date format in try catch When running on some phones it does not gets in catch and format works well and running on other phones it gets in catch but date is computed well . 我通过在try 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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM