Interface Clock


  • @ThreadSafe
    public interface Clock
    Interface for getting the current time.
    • Method Detail

      • now

        long now()
        Returns the current epoch timestamp in nanos from this clock. This timestamp should only be used to compute a current time. To compute a duration, timestamps should always be obtained using nanoTime(). For example, this usage is correct.
        
         long startNanos = clock.nanoTime();
         // Spend time...
         long durationNanos = clock.nanoTime() - startNanos;
         

        This usage is NOT correct.

        
         long startNanos = clock.now();
         // Spend time...
         long durationNanos = clock.now() - startNanos;
         
      • nanoTime

        long nanoTime()
        Returns a time measurement with nanosecond precision that can only be used to calculate elapsed time.