简体   繁体   English

为什么解析此日期字符串会引发不可解析的日期异常?

[英]why does parsing this date string throw an unparseable date exception?

I'm using SimpleDateFormat with the pattern EEE MM/dd hh:mma , passing in the date String Thu 10/9 08:15PM and it's throwing an Unparseable date exception. 我正在将SimpleDateFormat与模式EEE MM/dd hh:mma ,传入日期字符串Thu 10/9 08:15PM并引发了不可解析的日期异常。 Why? 为什么? I've used various patterns with SimpleDateFormat before so I'm fairly familiar with its usage. 之前,我已经在SimpleDateFormat使用了各种模式,所以我对它的用法非常熟悉。 Maybe I'm missing something obvious from staring at it too long. 盯着它看太久,也许我错过了一些明显的东西。

The other possibility is funky (technical term) whitespace. 另一种可能性是时髦的(技术术语)空白。 The context is a screen-scraping app, where I'm using HtmlCleaner to tidy up the messy html. 上下文是一个抓屏应用程序,我在其中使用HtmlCleaner整理凌乱的html。 While I've found HtmlCleaner to be pretty good overall, I've noticed strange issues with characters that look like whitespace but aren't recognized as such with a StringTokenizer, for example. 虽然我发现HtmlCleaner总体来说还不错,但是我注意到一些奇怪的问题,例如看起来像空格的字符,但StringTokenizer却无法识别。 I've mostly worked around it and haven't dug into the character encoding or anything like that but am starting to wonder. 我大部分时间都在解决它,还没有深入研究字符编码或类似的东西,但是开始感到奇怪。

To test if it's the date format, write a test class to prove it out. 要测试它是否为日期格式,请编写一个测试类来证明它。 For these types of things, I like to use bsh (beanshell). 对于这些类型的事情,我喜欢使用bsh(beanshell)。 Here was my test: 这是我的测试:

sdf = new java.text.SimpleDateFormat("EEE MM/dd hh:mma");
System.out.println(sdf.format(sdf.parse("Thu 10/9 08:15PM")));

Which outputted: Fri 10/09 08:15PM 输出:星期五10/09 08:15 PM

So, at least with my jdk / jre version (1.6), the format strings seem to work just fine. 因此,至少在我的jdk / jre版本(1.6)中,格式字符串似乎可以正常工作。 i think the next step is to make sure the string you're dealing with is exactly what you think it is. 我认为下一步是确保您要处理的字符串与您认为的完全相同。 Can you add logging to your code, and dump out the input string to a log file? 您可以将日志记录添加到代码中,然后将输入字符串转储到日志文件中吗? Then you could look at it in a nice text editor, run it through your test class, or look at it in a hex editor to make sure that it's just normal text. 然后,您可以在一个不错的文本编辑器中查看它,在测试类中运行它,或者在十六进制编辑器中查看它以确保它只是普通文本。

Good luck! 祝好运!

First question here on StackOverFlow so I'm not sure what the proper way to mark this resolved is. 这里是关于StackOverFlow的第一个问题,所以我不确定标记此已解决的正确方法是什么。 Most of the answers are in the comments of Eric's answer. 大多数答案都在Eric答案的评论中。

The root cause was a 'space' character in the date string that was not recognized as such. 根本原因是日期字符串中的“空格”字符未被识别。 It was a hex char of 'A0', which is a non-breaking space. 它是“ A0”的十六进制字符,这是一个不间断的空格。 I ended up converting the date string to a char array, checking the characters with Character.isSpaceChar(), and replacing those that returned true with a " " char. 我最终将日期字符串转换为char数组,使用Character.isSpaceChar()检查字符,并用“” char替换那些返回true的字符。

Try this instead for your pattern: 为您的模式尝试以下方法:

EEE MM/d hh:mma

The difference is the single d instead of double dd , since your date is for 10/9 instead of 10/09. 区别是单d而不是双dd ,因为您的日期是10/9而不是10/09。

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

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