This article will explain different ways in which nanoseconds or nanotime can be converted into seconds in java.
Method 1: Using TimeUnit.convert
java.util.concurrent.TimeUnit
is an enum in java that contains values representing different time units such as HOURS, MINUTES, SECONDS, MILLISECONDS, NANOSECONDS etc.
TimeUnit
is used to convert values between different time units. Its convert()
method can be used to convert a duration in nanoseconds to seconds.
convert()
takes 2 arguments
A. Duration to be converted.
B. Unit of this duration. In this case, it will be nanoseconds
Example,
long timeInNanoSeconds = 1000000000; long seconds = TimeUnit.SECONDS. convert(timeInNanoSeconds, TimeUnit.NANOSECONDS); System.out.println(timeInNanoSeconds + " in seconds is " + convert);
Note that the target time unit is the enum value on which convert()
is called. In this case, it is SECONDS.
Above example prints
1000000000 in seconds is 1
Method 2: Using TimeUnit.convert
TimeUnit
enum has an overloaded convert()
method that takes a single argument of type java.time.Duration
. Duration
object can be created using its static method ofNanos
which takes a long
value as argument, which is the time interval or the value to be converted to nanoseconds.
ofNanos()
returns a Duration
object representing the number of nanoseconds provided to it as argument. Example,
long timeInNanoSeconds = 1000000000L; // convert nanoseconds to seconds long seconds = TimeUnit.SECONDS.convert( Duration.ofNanos(timeInNanoSeconds)); System.out.println(timeInNanoSeconds + " in seconds is " + seconds);
Note that the target unit is the one on which convert() is called. This should be SECONDS if you want to convert to seconds.
Output of above example is
1000000000 in seconds is 1
Method 3: Using TimeUnit.toSeconds
TimeUnit
has a method toSeconds()
that takes a single value as argument and converts it to seconds.
If you are converting nanoseconds to seconds, then this method should be called on TimeUnit.NANOSECONDS
as shown below.
long timeInNanoSeconds = System.nanoTime(); // convert to seconds long seconds = TimeUnit.NANOSECONDS. toSeconds(timeInNanoSeconds); System.out.println(timeInNanoSeconds + " in seconds is " + seconds);
Output of this example is
611665932118900 in seconds is 611665
This method is simple mathematics based on following relation between nanoseconds and seconds.
1 Second = 1000000000 Nanosecond
which means that
1 Nanosecond = 1/1000000000 Second
So, in order to convert nanoseconds to second, divide it by 1000000000 as shown in the below example.
// store start time long startTime = System.nanoTime(); // halt for 10 seconds Thread.sleep(10000); // store end time long endTime = System.nanoTime(); // get difference long duration = endTime - startTime; // convert to nanoseconds double seconds = (double)duration/1000000000; System.out.println(duration + " in seconds is " + seconds);
Above example calculates two different times in nanoseconds using System.nanoTime()
, calculates the difference between them and then converts it into seconds by dividing with 100000000.
Note that a delay is injected by sleeping the current thread. In actual environment, this might represent a time consuming task.
Above program prints
5001456700 in seconds is 5.0014567
With this method, you can also calculate the time interval taken to perform a task.
Do not forget to hit the clap if the article was useful.