We are moving to Java 17 (correto17) from Java 11 (correto11). As part of this, we also have to upgrade Ubuntu to standard:6.0 from standard:4.0 in AWS as mentioned here.
We are observing that in Java 11 and Java 17 Instant.now() output is a bit different.
For example,
System.out.println("Instant: " + Instant.now());
is giving output like below
Instant: 2022-12-12T18:04:27.267229ZInstant: 2022-12-12T18:04:27.267229114ZCan someone let me know what is causing this? How can I make the behaviour same in both the cases?
Instant#now() obtains the current instant from the system clock. If the system clock returns a precision of only up to microseconds, the Instant#toString simply truncates the last three zeros from the nine digits. If the system on which you are running Java 17 (correto17) returns a precision of nanoseconds, you can truncate it to the precision of microseconds using Instant#truncatedTo(java.time.temporal.TemporalUnit).
public class Main {
public static void main(String args[]) {
Instant instant = Instant.now();
Instant truncatedToMicros = instant.truncatedTo(ChronoUnit.MICROS);
System.out.println(truncatedToMicros);
}
}
Learn more about the modern Date-Time API from Trail: Date Time.
Yes, and what we need to notice is that when we use MacOS, the time API will just works as 6 digs precision, but when we use Linux(such as Ubuntu), it will be 9 digs : )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With