SimpleDateFormat 未正确解析毫秒
背景:
在我的数据库表中,我有两个时间戳,
timeStamp1 = 2011-08-23 14:57:26.662
timeStamp2 = 2011-08-23 14:57:26.9
当我执行“ORDER BY TIMESTAMP ASC”时,timeStamp2被认为是更大的时间戳(这是正确的)。
要求:我需要获取这些时间戳的差异(timeStamp2 - timeStamp1)
我的实现:
public static String timeDifference(String now, String prev) {
try {
final Date currentParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(now);
final Date previousParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(prev);
long difference = currentParsed.getTime() - previousParsed.getTime();
return "" + difference;
} catch (ParseException e) {
return "Unknown";
}
}
答案应该是238ms,但返回的值是-653ms。 我不确定我做错了什么。有什么建议吗?
Background:
In my database table, I have two timestamps
timeStamp1 = 2011-08-23 14:57:26.662
timeStamp2 = 2011-08-23 14:57:26.9
When I do an "ORDER BY TIMESTAMP ASC", timeStamp2 is considered as the greater timestamp(which is correct).
Requirement: I need to get the difference of these timestamps (timeStamp2 - timeStamp1)
My implementation:
public static String timeDifference(String now, String prev) {
try {
final Date currentParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(now);
final Date previousParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(prev);
long difference = currentParsed.getTime() - previousParsed.getTime();
return "" + difference;
} catch (ParseException e) {
return "Unknown";
}
}
The answer should have been 238ms, but the value that is returned is -653ms.
I'm not sure what I'm doing wrong. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在解析的格式和使用的格式不匹配。您期望一个三位数字段,但只提供了一位数字。它采用
9
并假设您的意思是009
,而您想要的是900
。日期格式很复杂,当您以不同的格式证明日期时,它可能会以不同的方式解析它们。文档说
S
表示毫秒数,该字段中的数字是 9,因此它的行为正确。编辑:这个例子可能有助于
打印
The format you are parsing and the format uses doesn't match. You expect a three digit field and are only providing one digits. It takes
9
and assumes you mean009
when what you want is900
. Date formats are complicated and when you prove dates in a different format it may parse them differently to you.The documentation says
S
means the number of milli-seconds and the number in that field is 9, so it is behaving correctly.EDIT: This example may help
prints
我不完全确定,但 JavaDoc 指出了这一点:
这表示
2011-08-23 14:57:26.9
中的毫秒将被解析为9
而不是900
。添加尾随零可能有效:2011-08-23 14:57:26.900
。I'm not entirely sure, but the JavaDoc states this:
This indicates that the milliseconds from
2011-08-23 14:57:26.9
would be parsed as9
instead of900
. Adding the trailing zeros might work:2011-08-23 14:57:26.900
.我建议使用 Joda-Time。它可以正确处理这些情况。在以下示例中,毫秒被正确解析为 200ms。
I'd suggest using Joda-Time. It handles these situations properly. In the following example, the milliseconds are correctly parsed as 200ms.
我也遇到了同样的问题,日志文件中的时间太准确,只有 6 位数毫秒。解析时间相差高达 16 分钟!搞什么?
更改位数减少了错误的差异,并且多亏了这个线程,我可以识别问题:
由于
SimpleDateFormat
内部仅处理 3 位数字,我用一个小正则表达式删除了不必要的数字(忽略舍入错误,工作1 最多 n 位):感谢@Peter Lawrey 的回答,防止我发疯:-)
I had the same problem with too accurate time from my logfiles with 6 digit milliseconds. Parsing Time gave up to 16 minutes difference! WTF?
Changing the number of digits reduced the erroneous difference and thanks to this thread I could identify the problem:
As
SimpleDateFormat
internally handles only 3 digits I removed the unnecessary with a small regex (ignoring round-off errors, working for 1 up to n digits):Thanks to @Peter Lawrey for your answer, prevented me going insane :-)