SimpleDateFormat的:无法解析的日期异常 [英] SimpleDateFormat: unparseable date exception
问题描述
在寻找一些现有的职位后,我仍然无法得到我的SimpleDateFormat解析器的工作。这里是code:
After looking after several existing posts, I am still not able to get my SimpleDateFormat parser working. Here is the code:
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
try {
volcanoListDate = df.parse(currentValue);
} catch (ParseException e) {
Log.d("DEBUG", e.toString());
Log.d("DEBUG", currentValue);
}
我总是最后一个ParseException的。下面是调试消息的输出:
I always end up with a ParseException. Here is the output of the debug messages:
06-09 23:52:17.478:DEBUG / DEBUG(2436):java.text.ParseException:无法解析的日期:
06-09 23:52:17.478: DEBUG/DEBUG(2436): java.text.ParseException: Unparseable date:
06-09 23:52:17.478:DEBUG / DEBUG(2436):周三,2011年6月8日3时23分55秒-0500
06-09 23:52:17.478: DEBUG/DEBUG(2436): Wed, 08 Jun 2011 03:23:55 -0500
区域设置IST组和图形看起来还好。我在哪里错了?
Locale ist set and the pattern looks okay. Where am I wrong?
推荐答案
下面是解决方案:
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
try {
volcanoListDate = df.parse(currentValue.replaceAll("\\p{Cntrl}", ""));
} catch (ParseException e) {
Log.d("VOLCANO_DEBUG", e.toString());
Log.d("VOLCANO_DEBUG", currentValue);
}
最重要的变化是 .replaceAll(\\ p {} CNTRL,)从分析的字符串中删除控制字符。 奇怪的是,我看不到任何这些字符用记事本+ +中的XML,其中字符串是从。不过,显然有什么东西,它是现在的工作。
The important change is .replaceAll("\\p{Cntrl}", "") which removes control characters from the parsed string. The strange thing is, that I do not see any of those characters with Notepad++ in the xml where the string is from. However, obviously there is something and it is working now.
感谢所有帮助!
这篇关于SimpleDateFormat的:无法解析的日期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!