Friday, May 10, 2024
HomeProgramming3 Ways to Convert System.nanoTime to Seconds in Java

3 Ways to Convert System.nanoTime to Seconds in Java

How to convert System.nanoTime to seconds in Java:

In this post we will learn how to convert System.nanoTime Unpleasant seconds in Java in different ways. The System.nanoTime The method is used to obtain the current value of the JVM’s time source in nanoseconds. The return value of System.nanoTime should only be used to find elapsed time. It is not related to the system time.

System.nanoTime:

The nanoTime is a static method defined in the System class:

public static native long nanoTime();

It gives back time long format.

This method is usually used to find out the execution time in code. We can calculate this value in two places and find the difference to determine the execution time between the two points. Let’s learn how to use the nanoseconds value seconds in different ways in Java with examples.

Method 1: By division:

We know that 1 second is equal to 1000000000 nanoseconds. So if we divide the value of nanoseconds by 1000000000it returns the value in seconds.

public class Main {
    public static void main(String[] args) {
        long startTime = System.nanoTime();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long timeDiff = System.nanoTime() - startTime;
        double timeDiffInSeconds = (double) timeDiff / 1_000_000_000;

        System.out.println("Time difference in nanoseconds: " + timeDiff);
        System.out.println("Time difference in seconds: " + timeDiffInSeconds);
    }
}

In this example we have recorded the time at the beginning of the program. The program is stopped for 10 seconds by using Thread.sleep. It has recorded the time again and the time difference is being calculated.

The variable time is used to maintain the time difference in nanoseconds. This time we divided by 1000000000 to find the difference in seconds to which the value is assigned timeDifferenceInSeconds variable.

Running this program will print:

Time difference in nanoseconds: 10004324181
Time difference in seconds: 10.004324181

Method 2: By using TimeUnit.toSeconds():

The Time unit class provides a method to Seconds to convert a value to seconds. We can use this method to convert a value of nanoseconds to seconds.

This method returns the converted value long format.

public long toSeconds(long duration)

I’ll show you how it works with an example:

import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        long startTime = System.nanoTime();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long timeDiff = System.nanoTime() - startTime;
        long timeDiffInSeconds = TimeUnit.NANOSECONDS.toSeconds(timeDiff);

        System.out.println("Time difference in nanoseconds: " + timeDiff);
        System.out.println("Time difference in seconds: " + timeDiffInSeconds);
    }
}

This is similar to the first example. I use Time unit.NANOSECONDS.toSeconds method to convert the value from nanoseconds to seconds.

It prints:

Method 3: By using TimeUnit.convert():

There is one more method in the Time unit class called transfer() for time conversion. We can use it to the nanoseconds time to seconds. This method needs two parameters:

public long convert(long sourceDuration, TimeUnit sourceUnit)

The first parameter is the time in long that we want to convert. The second parameter is the unit of the source time we are converting. It returns the converted time long format.

import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        long startTime = System.nanoTime();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long timeDiff = System.nanoTime() - startTime;
        long timeDiffInSeconds = TimeUnit.SECONDS.convert(timeDiff, TimeUnit.NANOSECONDS);

        System.out.println("Time difference in nanoseconds: " + timeDiff);
        System.out.println("Time difference in seconds: " + timeDiffInSeconds);
    }
}

We pass the time difference in nanoseconds as the first parameter and Time unit.NANOSECONDS as second parameter. This method returns the time in seconds:

Time difference in nanoseconds: 10003272169
Time difference in seconds: 10
RELATED ARTICLES

2 COMMENTS

  1. Hello my loved one I want to say that this post is amazing great written and include almost all significant infos I would like to look extra posts like this

  2. Usually I do not read article on blogs however I would like to say that this writeup very compelled me to take a look at and do so Your writing taste has been amazed me Thanks quite nice post

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

бнанс Створити акаунт on The Interview Process in Clark, Pampanga
Binance注册奖金 on Venus in Scorpio