为什么解析此日期字符串会引发不可稀释的日期异常? [英] why does parsing this date string throw an unparseable date exception?

查看:148
本文介绍了为什么解析此日期字符串会引发不可稀释的日期异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SimpleDateFormat与模式 EEE MM / dd hh:mma ,传递日期String Thu 10/9 08:15 PM 并且它抛出一个不可抛出的日期异常。为什么?我已经使用各种模式与 SimpleDateFormat 之前,所以我很熟悉它的用法。也许我从盯着它的时候就想出了一些明显的东西。



另一种可能性是时髦(技术术语)的空白。上下文是一个屏幕截图应用程序,我使用HtmlCleaner来整理杂乱的HTML。虽然我发现HtmlCleaner的整体性能相当不错,但是我发现有一些奇怪的问题,比如说像String这样的StringTokenizer这样的字符看起来像是空格。我大部分都是围绕它进行的,没有挖掘出字符编码或任何类似的东西,但是开始怀疑。

解决方案

要测试是否是日期格式,请写一个测试类来证明它。对于这些类型的东西,我喜欢使用bsh(beanshell)。这是我的测试:

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


$ b $($)

$ b

所以,至少用我的jdk / jre版本(1.6),格式字符串似乎工作正常。我认为下一步是确保您处理的字符串正是您的想法。可以将日志记录添加到代码中,并将输入字符串转储到日志文件中吗?然后,您可以在一个漂亮的文本编辑器中查看它,通过测试类运行它,或者在十六进制编辑器中查看它,以确保它只是正常的文本。




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. Why? I've used various patterns with SimpleDateFormat before so I'm fairly familiar with its usage. 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. 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. 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). 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

So, at least with my jdk / jre version (1.6), the format strings seem to work just fine. 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!

这篇关于为什么解析此日期字符串会引发不可稀释的日期异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆