Splitting milliseconds of a date fraction in Java

I treat the date (obtained from the server) in Java, using this template: "yyyy-MM-dd'T'HH:mm:ss.SSS"

.

Incoming lines can be of the following types:

2015-01-01T00:00:00.561
2015-01-01T00:00:00.5

      

My question is about fractional milliseconds. I am having trouble figuring out if .5

the second line 5

or 500

ms. Because when I parse it with my template I get 500

ms. Everything seems to be in order, but one has to double check if there is any general contract to trim those zeros on the server side. I wouldn't ask if the server is back 2015-01-01T00:00:00.500

, but since .5

I'm not sure what is on the server side 5

or 500

ms.

UPDATE:

I just spoke to the server team, they confirmed .5

is .500

.

+3


source to share


2 answers


ISO 8601

This template YYYY-MM-DDTHH:MM:SS.SSS±HH:MM

,, is a string format as defined by the ISO 8601 standard .

Your specific use is missing an offset from UTC (plus / minus at the end). Without an offset, the value is "local time" meaning that it refers to any location, such as "Christmas starts at 2015-12-25T00: 00: 00".

Both the Joda-Time libraries and the java.time package use ISO 8601 as their default string generation / parsing.



Decimal point

Yes, the numbers after the dot are just a decimal fraction. The point is the decimal point. A value such as 0.5

, is the same as 0.500

, both mean one half second, 500 milliseconds.

So it 2015-01-01T12:34:56.7

matches 2015-01-01T12:34:56.700

.

0


source


@dana has a good point: 1.5 1.500 However, of course there is an easy way to check. Just look for some set of records in your log files and you can easily see the sequences and how the milliseconds are in those sequences. I guess it will 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 the order you provided. In this example, you can clearly see that this is the same value for milliseconds throughout.

It's easy to see it in one log file. If you have more, then they probably come from different servers and you cannot compare the times. Different servers have different times by definition. Especially in milliseconds. Even if the log files are in the same window, but in different applications, it is still not recommended to compare times literally. One log file can be locked within a few milliseconds and your timing will be different. All you can do is relational comparison (early / late) in a file from one source. All other cases may be invalid.

+1


source







All Articles