[英]SimpleDateFormat does not work on some android versions
我必须阅读以下格式的日期, 星期六,2013年1月19日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项目中或在Nexus和Htc上使用它,则此方法非常完美,但是当我将调试器附加到Samsung s3时,它会给出解析器异常。
我希望这适用于所有版本的android和手机。
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSSZ");
尝试这个:
在捕获中打印currentValue
和Locale.getDefaultLocale()
。 语言环境最有可能阻止解析currentValue。
至少您可以在具有完全相同参数的PC上尝试使用该代码。
我通过在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.