SimpleDateFormat:无法解析的日期异常

发布于 2024-11-14 18:59:53 字数 568 浏览 6 评论 0原文

在查看了几篇现有的帖子后,我仍然无法让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-11-21 18:59:53

这是解决方案:

            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 中用 Notepad++ 没有看到任何这些字符。然而,显然有一些东西现在正在发挥作用。

感谢您的帮助!

Here is the solution:

            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);
        }

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!

合久必婚 2024-11-21 18:59:53

检查您输入的非打印字符,例如制表符(而不是空格)等。有时,它无法解析的原因与数字的格式无关,而与意外字符有很大关系(即你不能总是看到)。

考虑到有些人已经报告“对我有用”(@谢谢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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文