Current time in microseconds in java

Viewed 243450

On a Unix system, is there a way to get a timestamp with microsecond level accuracy in Java? Something like C's gettimeofday function.

12 Answers

Use Instant to compute microseconds since Epoch:

val instant = Instant.now();
val currentTimeMicros = instant.getEpochSecond() * 1000_000 + instant.getNano() / 1000;
LocalDateTime.now().truncatedTo(ChronoUnit.MICROS)
Related