3

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
Nagendra Busam
  • 235
  • 1
  • 5
  • 19
  • Are you sure they exist? When I run your code with a debugger, the seconds only has 3 places, IE time = {LocalTime@784} "15:34:18.068" hour = 15 minute = 34 second = 18 nano = 68000000 – David Zimmerman Jul 30 '19 at 15:35
  • From another application I am passing a string w/ yyyy-MM-dd-HH.mm.ss.SSSSSS even last 3 digits of SSS have values. I am able to convert that particular string to LocalDateTime in desired format. It does have values. I am trying to get something similar for current date in UTC for that format – Nagendra Busam Jul 30 '19 at 16:26
  • This is not possible with Java8, use java9 or later. Check out this answer for more details. – Ravindra Ranwala Jul 31 '19 at 10:08
  • This sounds wrong: *LocalDateTime in certain format for my spring data jpa entity column*. For persistence you should not use `LocalDateTime` for a point in time but rather `Instant`. And you shouldn’t worry about the format. – Ole V.V. Aug 04 '19 at 09:18

1 Answers1

3

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.

Dorin Simion
  • 131
  • 5