Timestamp in ISO 8601 - the last 6 digits yyyy-MM-dd'T'HH:mm:ss.?

喜夏-厌秋 提交于 2021-01-02 07:55:50

问题


I have timestamps looking like this:

    2015-03-21T11:08:14.859831
    2015-03-21T11:07:22.956087

I read a Wiki article on ISO 8601, but did not get the meaning of the last 6 digits here.

I tried getting it down to milliseconds using "yyyy-MM-dd'T'HH:mm:ss.sss" or "yyyy-MM-dd'T'HH:mm:ss.ssssss". Is it just more precise than milliseconds - up to microseconds?


回答1:


Is it just more precise than milliseconds?

Yes, it's microseconds in this case.

ISO-8601 doesn't actually specify a maximum precision. It states:

If necessary for a particular application a decimal fraction of hour, minute or second may be included. If a decimal fraction is included, lower order time elements (if any) shall be omitted and the decimal fraction shall be divided from the integer part by the decimal sign specified in ISO 31-0, i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred sign. If the magnitude of the number is less than unity, the decimal sign shall be preceded by two zeros in accordance with 3.6.

The interchange parties, dependent upon the application, shall agree the number of digits in the decimal fraction. [...]

(You very rarely actually see comma as the decimal separator - at least, that's my experience.)

Unfortunately in my experience, parsing a value like this in Java 7 is tricky - there isn't a format specifier for "just consume fractional digits and do the right thing". You may find you need to manually chop the trailing 3 digits off before parsing as milliseconds.

As Java 8 supports a precision of nanoseconds, it's rather simpler - and in fact, the built-in ISO formatter can parse it fine:

import java.time.*;
import java.time.format.*;

public class Test {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        System.out.println(LocalDateTime.parse("2015-03-21T11:07:22.956087", formatter));
        
    }
}


来源:https://stackoverflow.com/questions/31477309/timestamp-in-iso-8601-the-last-6-digits-yyyy-mm-ddthhmmss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!