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

2

11

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?

Moody answered 17/7, 2015 at 13:45 Comment(2)
Are you trying to get the milliseconds or is the question about 'WHAT ARE THESE NUMBERS'?Transcendent
Seeing these in the MySQL 5.7 query log: "2018-02-02T15:39:15.125927Z". Parsing via Perl library Time::Moment, which works nicely.Soule
M
18

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));
        
    }
}
Mcmullan answered 17/7, 2015 at 13:51 Comment(0)
E
2

You do not need a DateTimeFormatter

java.time API is based on ISO 8601 and therefore you do not need a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format (e.g. your date-time string, 2015-03-21T11:08:14.859831).

Demo:

import java.time.LocalDateTime;

class Main {
    public static void main(String args[]) {
        String strDateTime = "2015-03-21T11:08:14.859831";
        LocalDateTime ldt = LocalDateTime.parse(strDateTime);
        System.out.println(ldt);
    }
}

Output:

2015-03-21T11:08:14.859831

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

It is a fraction of a second, to microsecond resolution.

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".

Your pattern is not correct. The correct letter for the fraction of second is S (capital S) i.e. if you want the value to the millisecond resolution, the pattern should be yyyy-MM-dd'T'HH:mm:ss.SSS. Check the DateTimeFormatter documentation to learn more about this.

Is it just more precise than milliseconds - up to microseconds?

Yes, it is. The java.time API can give you an even more precise resolution, to nanoseconds (depending on the system clock).

Learn more about the modern Date-Time API from Trail: Date Time.

Emmeram answered 25/12, 2022 at 22:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.