Get Current Value of System Timer in Nanoseconds



In this article, we use the System.nanoTime() method in Java to get the current value of the system timer in nanoseconds. It returns the current value of the most precise available system timer, in nanoseconds.

Problem Statement

Given that you need to get the current value of the system timer in nanoseconds, write a Java program that retrieves this value using the System.nanoTime() method.
Input
There is no specific input as the program retrieves the system's current time in nanoseconds directly.
Output
Current Value: 49908709882168 nanoseconds

Steps to get the current value of the system timer in nanoseconds

The following are the steps to get the current value of the system timer in nanoseconds ?
  • Use the System.nanoTime() method to get the current system timer value.
  • Store the returned value in a long variable.
  • Return the value of the current system timer value in nanoseconds

Java program to get the current value of the system timer in nanoseconds

The following is an example of finding the current value of the system timer in nanoseconds ?

public class Demo {
	public static void main(String[] args) {
		long res = System.nanoTime();
		System.out.println("Current Value: " + res + " nanoseconds");
	}
}

Output

Current Value: 49908709882168 nanoseconds

Code Explanation

The System.nanoTime() method is used to get the current value of the system timer. This method returns a long type value, representing the time in nanoseconds. The program then prints this value to the console, which displays the current timer value in nanoseconds.

Updated on: 2024-11-18T22:30:20+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements