问题
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
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.
UPDATE:
I just had a talk with the server team, they confirmed .5
is .500
.
回答1:
ISO 8601
That pattern, YYYY-MM-DDTHH:MM:SS.SSS±HH:MM
, is a string format defined by the ISO 8601 standard.
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".
Both the Joda-Time library and java.time package use ISO 8601 formats as their defaults in generating/parsing strings.
Decimal Point
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.
So, 2015-01-01T12:34:56.7
is the same as 2015-01-01T12:34:56.700
.
回答2:
@dana has good point: 1.5 is 1.500 However to be sure there is simple way to validate. Just look for some set of the records in your log files and you can easily see sequences and how milliseconds progress in these sequences. I assume that would be
2015-01-01T00:00:00.46
2015-01-01T00:00:00.5
2015-01-01T00:00:00.561
2015-01-01T00:00:00.57
2015-01-01T00:00:00.678
but not in order you had provided. Form this example you can clearly see that this the same meaning for milliseconds everywhere.
It's easy to see it in one log file. If you have more then probably they come from different servers and you cannot compare time. Different servers by definition have different time. Especially in milliseconds. Even if log files are from the same box but different applications it is still not good idea to compare time literally. One log file could be locked for some milliseconds and so your time will be different. All what you can do is relational comparison (early/later) in on file from one source. All other cases could be invalid.
来源:https://stackoverflow.com/questions/30087963/parsing-milliseconds-fraction-of-date-in-java