How can I print Java Instant
as a timestamp with fractional seconds like 1558766955.037 ? The precision needed is to 1/1000, as the example shows.
I tried (double) timestamp.getEpochSecond() + (double) timestamp.getNano() / 1000_000_000
, but when I convert it to string and print it, it shows 1.558766955037E9
.
The result you're seeing is the secientific (e-) notation of the result you wanted to get. In other words, you have the right result, you just need to properly format it when you print it:
Instant timestamp = Instant.now();
double d = (double) timestamp.getEpochSecond() + (double) timestamp.getNano() / 1000_000_000;
System.out.printf("%.2f", d);