Convert System.nanoTime to Seconds in Java

1. Introduction

In this article, we will look into How to Convert System.nanoTime() to Seconds in Java. We will look at different solutions to this problem in detail with working examples. Let us first have a quick look into the System.nanoTime() method in Java.

2. System.nanoTime() Method

The System.nanoTime() method is a static method of the System class and it gives the current value of the JVM’s most precise or high-resolution time source in nanoseconds. The value returned is of very high precision around 1/1000000th of a second.

The syntax of the method:

It returns the value of type Long. It is not thread-safe, the accuracy decreases if used between more than two threads.

Now, we outline different ways to Convert System.nanoTime() to Seconds in Java.

3. Dividing the System.nanoTime() with a Constant Value

To convert System.nanoTime to Seconds, we can divide the nanoseconds value returned by the nanoTime() method with a constant value of  1_000_000_000, this division will give the resultant value including decimals which must be typecasted to type double.

Let us look into the code.

Output:

4. Using the convert() Method of TimeUnit Class in Java

The TimeUnit class in Java represents time durations at a given unit and provides useful utility methods to convert across these units. It allows conversions to  Minutes , Seconds, Milliseconds, Nanoseconds and other units. It provides the convert() method that different durations across these time units.

The syntax of method:

  • Use the Static reference to the TimeUnit class to convert the System.nanoTime() and specify the timeunit to convert in this case we provide it as : SECONDS.
  • Pass sourceDuration as System.nanoTime() to convert() method.
  • Pass sourceUnit as TimeUnit.NANOSECONDS as SourceDuration is in Nano seconds Timeunit.

Let us look at the implementation in code.

Output:

In order to obtain a high precision value than integer, we can convert this value to type double to get higher precision up to 3 decimal places.

  • In such case we need to get this value in format TimeUnit.MILLISECONDS.
  • Then divide the value by 1000.0 to convert it into seconds and get the value with precision of up to 3 decimal places.

Let us look at the code snippet.

Output:

5. Using the toSeconds() Method of TimeUnit Class in Java

The TimeUnit class also provides the toSeconds() method that converts a given time duration unit to seconds. In order to convert the value, we need to specify the type of Source Time duration for e.g. NANOSECONDS , MILLISECONDS, etc.

The syntax of the method:

Let us look into the code.

Output:

6. Using the Utility Methods of Duration Class in Java

With the advent of Java 9, the Duration class was modified and some new features were introduced, this class models a unit of time in terms of seconds and nanoseconds and provides useful and common utility methods to perform operation on these time units.

It is present in java.time package library. Here, we can use the utility methods of these class to Convert System.nanoTime() to seconds.

  • The Duration class provides the getSeconds() method and toSeconds() method respectively to convert a time unit to its equivalent value in seconds.
  • Use the ofNanos() method of the Duration class to specify our time unit to be in format of Nanoseconds and pass the System.nanotime() as a argument to this method.

The syntax of the methods :

Let us look at the implementation of this approach in code.

Output:

7. Conclusion

We listed 4 different ways on How to Convert System.nanoTime() to Seconds in Java in detail with working examples. You can try them out with code in your Local IDE for a clear understanding.

Feel free to reach out to us for any queries/suggestions.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *