SimpleDateFormat:无法解析的日期异常
在查看了几篇现有的帖子后,我仍然无法让我的 SimpleDateFormat 解析器工作。这是代码:
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 结束。这是调试消息的输出:
06-09 23:52:17.478: 调试/调试(2436): java.text.ParseException: 无法解析的日期:
06-09 23:52:17.478:调试/调试(2436):2011年6月8日星期三03:23:55 -0500
区域设置已设置并且模式看起来不错。我哪里错了?
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);
}
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: Unparseable date:
06-09 23:52:17.478: DEBUG/DEBUG(2436): Wed, 08 Jun 2011 03:23:55 -0500
Locale ist set and the pattern looks okay. Where am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是解决方案:
重要的更改是 .replaceAll("\\p{Cntrl}", "") ,它从解析的字符串中删除控制字符。
奇怪的是,我在字符串所在的 xml 中用 Notepad++ 没有看到任何这些字符。然而,显然有一些东西现在正在发挥作用。
感谢您的帮助!
Here is the solution:
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.
Thanks for all the help!
检查您输入的非打印字符,例如制表符(而不是空格)等。有时,它无法解析的原因与数字的格式无关,而与意外字符有很大关系(即你不能总是看到)。
考虑到有些人已经报告“对我有用”(@谢谢Bozho!)我强烈怀疑你的输入字符串中存在不可打印的字符。谁知道,您可能会在某个地方嵌入一个垂直选项卡!
Check your input for non-printing characters, like tab (instead of space), etc. Sometimes the reason it can't parse has little to do with the formatting of the numbers and a lot to do with unexpected characters (that you can't always see).
Considering some people have already reported "works for me" (@Thanks Bozho!) I would strongly suspect unprintable characters in your input string. Who knows, you might have a vertical tab embedded in there somewhere!