TimeUnit toNanos() method in Java with Examples Last Updated : 17 Feb, 2021 Comments Improve Suggest changes Like Article Like Report 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 // Java program to demonstrate // toNanos() method of TimeUnit Class import java.util.concurrent.*; import java.util.Date; class GFG { public static void main(String args[]) { // Get current time in milliseconds long timeInMilliSec = new Date().getTime(); // Create a TimeUnit object TimeUnit time = TimeUnit.MILLISECONDS; // Convert milliseconds to NanoSeconds // using toNanos() method System.out.println("Time " + timeInMilliSec + " milliSeconds in NanoSeconds = " + time.toNanos(timeInMilliSec)); } } Output: Time 1539585501608 milliSeconds in NanoSeconds = 1539585501608000000 Program 2: Java // Java program to demonstrate // toNanos() method of TimeUnit Class import java.util.concurrent.*; import java.util.Calendar; class GFG { public static void main(String args[]) { // Get current time in milliseconds long timeInMilliSec = Calendar .getInstance() .getTimeInMillis(); // Create a TimeUnit object TimeUnit time = TimeUnit.MILLISECONDS; // Convert milliseconds to NanoSeconds // using toNanos() method System.out.println("Time " + timeInMilliSec + " milliSeconds in NanoSeconds = " + time.toNanos(timeInMilliSec)); } } Output: Time 1539585503771 milliSeconds in NanoSeconds = 1539585503771000000 Comment More infoAdvertise with us Next Article TimeUnit toNanos() method in Java with Examples R RishabhPrabhu Follow Improve Article Tags : Java Practice Tags : Java Similar Reads TimeUnit toDays() method in Java with Examples The toDays() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Days, since midnight UTC on the 1st January 1970. It is equivalent to DAYS.convert(duration, this). Syntax: public long toDays(long duration) Parameters: This method accepts a mandatory 2 min read TimeUnit toHours() method in Java with Examples The toHours() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Hours, since midnight UTC on the 1st January 1970. Syntax: public long toHours(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration in mi 2 min read TimeUnit toMicros() method in Java with Examples The toMicros() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of MicroSeconds, since midnight UTC on the 1st January 1970. Syntax: public long toMicros(long duration) Parameters: This method accepts a mandatory parameter duration which is the durat 2 min read TimeUnit toMinutes() method in Java with Examples The toMinutes() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Minutes, since midnight UTC on the 1st January 1970. Syntax: public long toMinutes(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration 2 min read TimeUnit toSeconds() method in Java with Examples The toSeconds() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Seconds, since midnight UTC on the 1st January 1970. Syntax: public long toSeconds(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration 2 min read Like