简体   繁体   English

SimpleDateFormat:无法解析的日期异常

[英]SimpleDateFormat: unparseable date exception

After looking after several existing posts, I am still not able to get my SimpleDateFormat parser working.在查看了几个现有的帖子后,我仍然无法让我的 SimpleDateFormat 解析器工作。 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.我总是以 ParseException 告终。 Here is the output of the debug messages:这是调试消息的 output:

06-09 23:52:17.478: DEBUG/DEBUG(2436): java.text.ParseException: Unparseable date: 06-09 23:52:17.478:调试/调试(2436):java.text.ParseException:无法解析的日期:
06-09 23:52:17.478: DEBUG/DEBUG(2436): Wed, 08 Jun 2011 03:23:55 -0500 06-09 23:52:17.478:调试/调试(2436):2011 年 6 月 8 日,星期三 03:23:55 -0500

Locale ist set and the pattern looks okay.区域设置和模式看起来不错。 Where am I wrong?我哪里错了?

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.重要的变化是.replaceAll("\\p{Cntrl}", "")从解析的字符串中删除控制字符。 The strange thing is, that I do not see any of those characters with Notepad++ in the xml where the string is from.奇怪的是,我在字符串来自的 xml 中没有看到任何带有 Notepad++ 的字符。 However, obviously there is something and it is working now.但是,显然有一些东西,它现在正在工作。

Thanks for all the help!感谢所有的帮助!

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).检查您输入的非打印字符,如制表符(而不是空格)等。有时它无法解析的原因与数字的格式无关,而与意外字符有很大关系(你可以' t 总是看到)。

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!考虑到有些人已经报告“为我工作”(@Thanks Bozho。)我强烈怀疑您的输入字符串中有不可打印的字符,谁知道,您可能在某处嵌入了一个垂直选项卡!

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

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