Current Time in Microseconds in Java

Current time in microseconds in java

No, Java doesn't have that ability.

It does have System.nanoTime(), but that just gives an offset from some previously known time. So whilst you can't take the absolute number from this, you can use it to measure nanosecond (or higher) precision.

Note that the JavaDoc says that whilst this provides nanosecond precision, that doesn't mean nanosecond accuracy. So take some suitably large modulus of the return value.

How to get current time in nano seconds in java?

Finally, Thanks for Mr. Franz Wilhelmstötter

I found one solution. using http://jenetics.io/ and
Class call NanoClock.java is converting and doing the same trick that Stephen C suggested. I want to share this because it will useful for others as well. I am not able to confirm that is given precise nano time, but this trick works for me. @Ole V.V. Thanks again for your help.

Create Java DateTime Instant from microseconds

    long timeMicros = 1_565_245_051_795_306L;
Instant i = Instant.EPOCH.plus(timeMicros, ChronoUnit.MICROS);
System.out.println(i);

Output is:

2019-08-08T06:17:31.795306Z

Edit: Rather than dividing and multiplying to convert microseconds to milliseconds and/or seconds I preferred to use the built-in support for microseconds. Also when explicitly adding them to the epoch feels a little hand-held.

You already know how to convert Instant to LocalDateTime, you’ve shown it in the question, so I am not repeating that.

Edit:

Do you have a solution to get the timeMicros back from the Instant?

There are a couple of options. This way the calculation is not so complicated, so I might do:

    long microsBack = TimeUnit.SECONDS.toMicros(i.getEpochSecond())
+ TimeUnit.NANOSECONDS.toMicros(i.getNano());
System.out.println(microsBack);

1565245051795306

To be more in style with the first conversion you may prefer the slightly shorter:

    long microsBack = ChronoUnit.MICROS.between(Instant.EPOCH, i);

Edit: Possibly nit-picking, but also to avoid anyone misunderstanding: LocalDateTime has had nanosecond precision always. Only the now method had millisecond precision on Java 8. I read somewhere that from Java 9 the precision varies with the platform, but you are right, microsecond precision seems typical.

How to get the current time of microsecond accuracy

tl;dr

Java 9+ captures current moment in microseconds, while Java 8 captures in mere milliseconds.

ZonedDateTime
.now() // Uses JVM’s current default time zone.
.format(
DateTimeFormatter
.ofLocalizedDateTime( FormatStyle.FULL ) // Uses JVM’s current default locale.
)

ZonedDateTime.now

I cannot image a scenario where calling LocalDateTime.now() is the right thing to do. That class cannot represent a moment as it lacks the context of a time zone or offset. Use ZonedDateTime.

ZonedDateTime zdt = ZonedDateTime.now( ZoneId.systemDefault() ) ;

In Java 8, the moment was captured with a resolution of milliseconds. In Java 9+, microseconds. In all versions, the java.time types are capable of representing nanoseconds, but conventional computers lack the hardware clocks to accurately capture current moment in nanos.

Generate text representing that object’s value in standard ISO 8601 format extended by appending the name of the time zone in square brackets.

String output = zdt.toString() ;

Generate localized text.

Locale locale = Locale.CANADA_FRENCH ;  // Or Locale.getDefault()
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG ).withLocale( locale ) ;
String output = zdt.format( f ) ;

Java - get unix epoch with microsecond

You can easily do it using standard java time Instant + a number format. Keep in mind the result will be in UTC, though. Here is a draft with current time:

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.Instant;

Instant datetime = Instant.now();

// Extract needed information: date time as seconds + fraction of that second
long secondsFromEpoch = datetime.getEpochSecond();
int nanoFromBeginningOfSecond = datetime.getNano();
double nanoAsFraction = datetime.getNano()/1e9;

// Now, let's put that as text
double epochSecondUTCPlusNano = secondsFromEpoch + nanoAsFraction;
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(6);
format.setMaximumFractionDigits(6);
format.setGroupingUsed(false);
System.out.print(format.format(epochSecondUTCPlusNano));


Related Topics



Leave a reply



Submit