java.text.ParseException:无法解析的日期:yyyy-MM-dd HH:mm:ss.SSSSSS
我收到以下代码的 ParseException
String dateStr = "2011-12-22 10:56:24.389362";
String formatStr = "yyyy-MM-dd HH:mm:ss.SSSSSS";
Date testDate = null;
SimpleDateFormat sdf= new SimpleDateFormat(formatStr);
sdf.setLenient(false);
testDate = sdf.parse(dateStr);
System.out.println("CHECK DATE " + sdf.format(testDate));
Exception in thread "main" java.text.ParseException: Unparseable date: "2011-12-22 10:56:24.389362"
在 java.text.DateFormat.parse(DateFormat.java:337)
如果我注释掉行 sdf.setLenient(false)
,那么我会在输出中看到时间差异
检查日期 2011-12-22 11:02:53.000362
我做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“S”代表毫秒。一秒有 1000(0 到 999)毫秒。 389362 大于 999。额外的 389000 毫秒将转换为 389 秒,即 6 分 29 秒并添加到时间中。
'S' is for millisecond. There are 1000 (0 to 999) milliseconds in a second. 389362 is greater than 999. The extra 389000 milliseconds are getting converted to 389 seconds, or 6 minutes 29 seconds and added to the time.
S
格式说明符指的是毫秒。当您允许宽松的解析时,最后一部分将被解释为 389362 毫秒。当它被添加到迄今为止的日期时,最后 3 位数字(实际上,值 % 1000)成为实际的毫秒,并且您最终得到的日期比您预期晚了大约 389 秒(约 6 1/2 分钟) 。 (通过严格的解析,解析器知道 389362 毫秒没有意义,因此会抛出错误。)如果你能保证日期总是这样,那么最简单的方法就是砍掉最后 3 位数字离开。 (这大约有一半的时间会给你一个相差一毫秒的日期。但这比必须编写一个日期解析器要好......)
The
S
format specifier refers to milliseconds. When you allow lenient parsing, the last part is interpreted as 389362 milliseconds. When that's added to the date so far, the last 3 digits (actually, the value % 1000) become the actual milliseconds, and you wind up with a date about 389 seconds (~6 1/2 minutes) later than you're expecting. (With strict parsing, the parser knows that 389362 milliseconds doesn't make sense, so it throws an error.)The simplest way around that, if you can guarantee the date will always look like that, would be to chop the last 3 digits off. (This will about half the time give you a date that's off by a millisecond. But that's better than having to write a date parser...)
您输入的日期(毫秒)不正确。它应该是:-
String dateStr = "2011-12-22 10:56:24.389";
您也不需要模式中额外数量的“S”。以下内容就足够了:
String formatStr = "yyyy-MM-dd HH:mm:ss.S";
在 java 文档 用于
Number
的表示类型:当您将 lenient 设置为 true(或注释掉默认为 true 的行)时它会起作用,因为您要求解析器对解析不严格。来自 setLenient() 上的 java 文档:-
Your date input for milliseconds is incorrect. It should be:-
String dateStr = "2011-12-22 10:56:24.389";
You also do not need the extra number of "S"s in the pattern. The following should suffice:
String formatStr = "yyyy-MM-dd HH:mm:ss.S";
It is clearly mentioned in the java docs for presentation type of
Number
:It works when you set lenient to be true (or comment out the line which defaults it true) since you are asking the parser to be not strict about the parsing. From java docs on setLenient():-
S
仅用于 毫秒。如果您想要微秒,则必须编写自己的解析器。S
is only to be used for milliseconds. If you want microseconds, you will have to write your own parser.使用
toISOString('HH:mm:ss.S')
获取毫秒(3 位数字),然后根据需要用 0 补全。例如:
返回 "2012-02-10T12:16:39.124 Z”
Use
toISOString('HH:mm:ss.S')
to get milliseconds (3 digits), then complete as you need with 0.For example:
returns "2012-02-10T12:16:39.124Z"