Table of Contents
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:
1 2 3 |
public static long nanoTime() |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class ConvertNanotimeToSeconds{ public static void main(String[] args) { // get current system timestamp in nanoseconds long nanoSeconds = System.nanoTime(); System.out.println("The current system timestamp in nanoseconds: "+nanoSeconds+"\n"); // divide to seconds and typecast to double double seconds = (double) nanoSeconds/1_000_000_000; System.out.println("The current system timestamp in seconds: "+seconds); } } |
Output:
1 2 3 4 5 |
The current system timestamp in nanoseconds: 2343310746135100 The current system timestamp in seconds: 2343310.7461351 |
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:
1 2 3 |
public long convert(long sourceDuration, TimeUnit sourceUnit) |
- 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() toconvert()
method. - Pass
sourceUnit
as TimeUnit.NANOSECONDS asSourceDuration
is in Nano seconds Timeunit.
Let us look at the implementation in code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.concurrent.TimeUnit; public class ConvertNanotimeToSeconds{ public static void main(String[] args) { // get current system timestamp in nanoseconds long nanoSeconds = System.nanoTime(); System.out.println("The current system timestamp in nanoseconds: "+nanoSeconds+"\n"); // Use TimeUnit.SECONDS to convert in seconds. long seconds = TimeUnit.SECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS); System.out.println("The current system timestamp in seconds: "+seconds); } } |
Output:
1 2 3 4 5 |
The current system timestamp in nanoseconds: 2463358120387600 The current system timestamp in seconds: 2463358 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.concurrent.TimeUnit; public class ConvertNanotimeToSeconds{ public static void main(String[] args) { // get current system timestamp in nanoseconds long nanoSeconds = System.nanoTime(); System.out.println("The current system timestamp in nanoseconds: "+nanoSeconds+"\n"); // Use TimeUnit.MILLISECONDS and divide by 1000.0 to convert in seconds. double seconds = TimeUnit.MILLISECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS)/1000.0; System.out.println("The current system timestamp in seconds: "+seconds); } } |
Output:
1 2 3 4 5 |
The current system timestamp in nanoseconds: 2503502543334700 The current system timestamp in seconds: 2503502.543 |
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:
1 2 3 |
public long toSeconds(long duration) |
Let us look into the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.concurrent.TimeUnit; public class ConvertNanotimeToSeconds{ public static void main(String[] args) { // get current system timestamp in nanoseconds long nanoSeconds = System.nanoTime(); System.out.println("The current system timestamp in nanoseconds: "+nanoSeconds+"\n"); // use the toSeconds() method and specify the source type of argument. long seconds = TimeUnit.NANOSECONDS.toSeconds(nanoSeconds); System.out.println("The current system timestamp in seconds: "+seconds); } } |
Output:
1 2 3 4 5 |
The current system timestamp in nanoseconds: 2510279744152400 The current system timestamp in seconds: 2510279 |
Further reading:
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 :
1 2 3 4 5 |
public long getSeconds() public long toSeconds​() |
Let us look at the implementation of this approach in code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.time.Duration; public class ConvertNanotimeToSeconds{ public static void main(String[] args) { // get current system timestamp in nanoseconds long nanoSeconds = System.nanoTime(); System.out.println("The current system timestamp in nanoseconds: "+nanoSeconds+"\n"); // use the toSeconds() method and ofNanos() to specify Nanoseconds unit long seconds1 = Duration.ofNanos(nanoSeconds).toSeconds(); System.out.println("The current system timestamp in seconds using toSeconds() method: "+seconds1+"\n"); // use the getSeconds() method long seconds2 = Duration.ofNanos(nanoSeconds).getSeconds(); System.out.println("The current system timestamp in seconds using getSeconds() method: "+seconds2+"\n"); } } |
Output:
1 2 3 4 5 6 7 |
The current system timestamp in nanoseconds: 2514023646228500 The current system timestamp in seconds using toSeconds() method: 2514023 The current system timestamp in seconds using getSeconds() method: 2514023 |
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.