Nagendra Busam
Nagendra Busam

Reputation: 255

Local date time in certain format yyyy-MM-dd-HH.mm.ss.SSSSSS

I am trying to get LocalDateTime in certain format for my spring data jpa entity column.

I am not getting last 3 digits of millis/micros. I am not sure exactly what to call it.

I am always getting 000 for last 3 SSS portion even If I format

final String YYYY_MM_DD_HH_MM_SS_SSSSSS = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
    ZonedDateTime zdtAtUtc = ZonedDateTime.now(ZoneId.of("UTC"));

    LocalDateTime ldt = zdtAtUtc.toLocalDateTime();
    DateTimeFormatter destFormatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_SSSSSS);

    System.out.println(zdtAtUtc);
    System.out.println(zdtAtUtc.format(destFormatter));
    System.out.println(ldt);

Output

2019-07-30T15:23:18.232Z[UTC]
2019-07-30-15.23.18.232000
2019-07-30T15:23:18.232

Upvotes: 3

Views: 2769

Answers (1)

Dorin Simion
Dorin Simion

Reputation: 131

This is clock precision in Java 8.

If you'll check the code of now() method, it use behind an instant obtained like this: Instant now = clock.instant();

In Java 8 output for this instant: 2019-08-02T20:44:35.722Z

In later versions: 2019-08-02T20:39:27.343408800Z

So if you need more precision, you need to switch to a newer version of java.

Upvotes: 3

Related Questions