Interface Clock


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

    Modifier and Type
    Method
    Description
    static Clock
    Returns a default Clock which reads from system time.
    long
    Returns a time measurement with nanosecond precision that can only be used to calculate elapsed time.
    long
    now()
    Returns the current epoch timestamp in nanos from this clock.
  • Method Details

    • getDefault

      static Clock getDefault()
      Returns a default Clock which reads from system time.
    • 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.