Java.lang.System.nanoTime() Method

Advertisements


Description

The java.lang.System.nanoTime() method returns the current value of the most precise available system timer, in nanoseconds. The value returned represents nanoseconds since some fixed but arbitrary time (in the future, so values may be negative) and provides nanosecond precision, but not necessarily nanosecond accuracy.

Declaration

Following is the declaration for java.lang.System.nanoTime() method

public static long nanoTime()

Parameters

  • NA

Return Value

This method returns the current value of the system timer, in nanoseconds.

Exception

  • NA

Example

The following example shows the usage of java.lang.System.nanoTime() method.

package com.tutorialspoint;

import java.lang.*;

public class SystemDemo {

   public static void main(String[] args) {

      // returns the current value of the system timer, in nanoseconds
      System.out.print("time in nanoseconds = ");
      System.out.println(System.nanoTime());
    
      // returns the current value of the system timer, in milliseconds
      System.out.print("time in milliseconds = ");
      System.out.println(System.currentTimeMillis());
  }
}  

Let us compile and run the above program, this will produce the following result:

time in nanoseconds = 255073580723571
time in milliseconds = 1349311227921



Advertisements
Advertisements