java.time.DateTimeFormatter : Need ISO_INSTANT that always renders milliseconds

OK, I looked at the the source code and it’s pretty straightforward:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendInstant(3).toFormatter();

I hope it works for all scenarios, and it can help someone else. Don’t hesitate to add a better/cleaner answer.

Just to explain where it comes from, in the JDK’s code,

ISO_INSTANT is defined like this:

public static final DateTimeFormatter ISO_INSTANT;
static {
    ISO_INSTANT = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendInstant()
            .toFormatter(ResolverStyle.STRICT, null);
}

And DateTimeFormatterBuilder::appendInstant is declared as:

public DateTimeFormatterBuilder appendInstant() {
    appendInternal(new InstantPrinterParser(-2));
    return this;
}

And the constructor InstantPrinterParser signature is:

InstantPrinterParser(int fractionalDigits)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)