TimeUnit toNanos() method in Java with Examples
Last Updated :
17 Feb, 2021
The toNanos() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of NanoSeconds, since midnight UTC on the 1st January 1970.
Syntax:
public long toNanos(long duration)
Parameters: This method accepts a mandatory parameter duration which is the duration in milliSeconds.
Return Value: This method returns the converted duration as NanoSeconds.
Below program illustrate the implementation of TimeUnit toNanos() method:
Program 1:
Java
import java.util.concurrent.*;
import java.util.Date;
class GFG {
public static void main(String args[])
{
long timeInMilliSec = new Date().getTime();
TimeUnit time = TimeUnit.MILLISECONDS;
System.out.println( "Time " + timeInMilliSec
+ " milliSeconds in NanoSeconds = "
+ time.toNanos(timeInMilliSec));
}
}
|
Output:
Time 1539585501608 milliSeconds in NanoSeconds = 1539585501608000000
Program 2:
Java
import java.util.concurrent.*;
import java.util.Calendar;
class GFG {
public static void main(String args[])
{
long timeInMilliSec = Calendar
.getInstance()
.getTimeInMillis();
TimeUnit time = TimeUnit.MILLISECONDS;
System.out.println( "Time " + timeInMilliSec
+ " milliSeconds in NanoSeconds = "
+ time.toNanos(timeInMilliSec));
}
}
|
Output:
Time 1539585503771 milliSeconds in NanoSeconds = 1539585503771000000
Share your thoughts in the comments
Please Login to comment...