TimeUnit convert() method in Java with Examples Last Updated : 15 Oct, 2018 Comments Improve Suggest changes Like Article Like Report The convert() method of TimeUnit Class is used to convert the given time duration in the given unit to this unit. Since conversion involves from larger to smaller or smaller to larger units, loss of precision and overflow can occur while using this method. Syntax: public long convert(long sourceDuration, TimeUnit sourceUnit) Parameters: This method accepts two mandatory parameters: sourceDuration- which is the time duration in the given sourceUnit sourceUnit- which is the unit of the sourceDuration argument Return Value: This method returns the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow. Below program illustrate the implementation of TimeUnit convert() method: Program 1: To convert Minutes to MilliSeconds Java // Java program to demonstrate // convert() method of TimeUnit Class import java.util.concurrent.*; import java.util.Date; class GFG { public static void main(String args[]) { // Get time to be converted in Minutes long timeInMinutes = 55L; // Create a TimeUnit object TimeUnit time = TimeUnit.MILLISECONDS; // Convert Minutes to milliseconds // using convert() method System.out.println("Time " + timeInMinutes + " minutes in milliSeconds = " + time.convert(timeInMinutes, TimeUnit.MINUTES)); } } Output: Time 55 minutes in milliSeconds = 3300000 Program 2: To convert Seconds to Minutes Java // Java program to demonstrate // convert() method of TimeUnit Class import java.util.concurrent.*; import java.util.Date; class GFG { public static void main(String args[]) { // Get time to be converted in Seconds long timeInSec = 300L; // Create a TimeUnit object TimeUnit time = TimeUnit.MINUTES; // Convert Seconds to Minutes // using convert() method System.out.println("Time " + timeInSec + " seconds in minutes = " + time.convert(timeInSec, TimeUnit.SECONDS)); } } Output: Time 300 seconds in minutes = 5 Comment More infoAdvertise with us Next Article TimeUnit convert() 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 toNanos() method in Java with Examples 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 2 min read TimeUnit toMillis() method in Java with Examples The toMillis() 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 toMillis(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration i 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 Like