用Java解析日期的毫秒部分 [英] Parsing milliseconds fraction of date in Java
问题描述
我正在使用以下模式在Java中解析日期(从服务器获取):"yyyy-MM-dd'T'HH:mm:ss.SSS"
.
I am parsing dates (got from server) in Java using this pattern: "yyyy-MM-dd'T'HH:mm:ss.SSS"
.
传入字符串可能是以下类型:
Incoming strings may be of these types:
2015-01-01T00:00:00.561
2015-01-01T00:00:00.5
我的问题是毫秒数.我在弄清楚第二个字符串中的 .5
是 5
还是 500
ms时遇到麻烦.因为当我使用模式解析它时,会得到 500
ms.似乎还可以,但是需要仔细检查服务器端是否有任何共同的契约来修整那些零.我不问服务器是否返回 2015-01-01T00:00:00.500
,但是对于 .5
,我不确定服务器端 5
或 500
毫秒.
My question is about milliseconds fraction. I am having trouble figuring out whether the .5
in the second string is 5
or 500
ms. Because when I parse it using my pattern I get 500
ms. Seems OK, but need to double check if there is any common contract to trim those zeros on server side. I would not ask if server returned 2015-01-01T00:00:00.500
, but with .5
I am not sure what is on server side 5
or 500
ms.
更新:
我刚刚与服务器团队进行了交谈,他们确认 .5
是 .500
.
I just had a talk with the server team, they confirmed .5
is .500
.
推荐答案
ISO 8601
该模式 YYYY-MM-DDTHH:MM:SS.SSS±HH:MM
是您的特定用法忽略了 UTC 的偏移量(末尾的加号/减号).如果不使用偏移量,则该值是本地时间",表示任何本地位置,例如圣诞节始于2015-12-25T00:00:00".
Your particular usage omits the offset from UTC (the plus/minus at the end). Without the offset, the value is a "local time" meaning it refers to any locality such as "Christmas starts at 2015-12-25T00:00:00".
Joda-Time 库和
Both the Joda-Time library and java.time package use ISO 8601 formats as their defaults in generating/parsing strings.
是的,点后的数字确实只是十进制小数.点是小数点.诸如 0.5
的值与 0.500
相同,均表示半秒的时间,即500毫秒.
Yes, the digits after the dot are indeed simply a decimal fraction. The dot is a decimal point. A value such as 0.5
is the same as 0.500
, both mean one half of a second, 500 milliseconds.
因此, 2015-01-01T12:34:56.7
与 2015-01-01T12:34:56.700
相同.
这篇关于用Java解析日期的毫秒部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!