TimeUnit Class in Java with Examples

29 Sep, 2021
TimeUnit is an Enum available in java.util.concurrent package. TimeUnit as the name implies deals with Time units. TimeUnit provides time representation at a given unit of granularity.  It makes available methods to convert time across time units. TimeUnit is useful to know how a given time should be interpreted that is which time unit should be considered. Minute differences between time durations such as in microseconds and nanoseconds can be figured out using TimeUnit. It is used to perform timing and delay operations.
It supports nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and days units. For these units, TimeUnit specifies corresponding enum constants:
Nanoseconds: One thousandth of a microsecondMicroseconds: One thousandth of a millisecondMilliseconds: One thousandth of a secondSeconds: One secondMinutes: Sixty secondsHours: Sixty minutesDays: Twenty four hours
Nanoseconds: One thousandth of a microsecond
Microseconds: One thousandth of a millisecond
Milliseconds: One thousandth of a second
Seconds: One second
Minutes: Sixty seconds
Hours: Sixty minutes
Days: Twenty four hours
Example 1:
Java
// Java program to demonstrate TimeUnit Class import java.util.concurrent.TimeUnit; public class GFG {    public static void main(String args[])    {        long hours = 96;         // Convert given time (hours)in days        long days = TimeUnit.DAYS.convert(hours, TimeUnit.HOURS);         // Convert days in minutes        long minutes = TimeUnit.MINUTES.convert(days, TimeUnit.DAYS);         System.out.println(hours + " Hours = " + days                           + " Days = " + +minutes                           + " Minutes");         // Convert given time (minutes) to microseconds        long micros = TimeUnit.MINUTES.toMicros(minutes);        System.out.println(minutes + " Minutes = " + micros                           + " Microseconds");         // Convert given time (microseconds) to seconds        long seconds = TimeUnit.MICROSECONDS.toSeconds(micros);        System.out.println(micros + " Microseconds = "                           + seconds + " Seconds");         // Create TimeUnit object of type Minutes        TimeUnit time = TimeUnit.valueOf("MINUTES");        System.out.println("TimeUnit object type: " + time);    }}
96 Hours = 4 Days = 5760 Minutes
5760 Minutes = 345600000000 Microseconds
345600000000 Microseconds = 345600 Seconds
TimeUnit object type: MINUTES
Example 2: 
Java
// Java program to demonstrate TimeUnit Class import java.util.concurrent.TimeUnit; public class GFG implements Runnable {    public void run()    {        // Get array of TimeUnit enum constants using        // values()        for (TimeUnit unit : TimeUnit.values()) {            try {                               // pause for 1 second                TimeUnit.SECONDS.sleep(1);            }            catch (InterruptedException e) {                e.printStackTrace();            }             System.out.println(                unit + " : "                + unit.convert(24, TimeUnit.HOURS));        }    }    public static void main(String args[])    {        GFG obj1 = new GFG();        System.out.println("TimeUnit Example");               // Create and start Thread        Thread t1 = new Thread(obj1);        t1.start();               System.out.println("Now, thread will run for 5 seconds");        try {                       // Specify Thread join time, here, 5 seconds            TimeUnit.SECONDS.timedJoin(t1, 5);        }        catch (InterruptedException e) {            e.printStackTrace();        }               System.out.println("Thread  Execution Paused");        System.out.println("Resuming Thread Execution...");    }}
TimeUnit Example
Now, thread will run for 5 seconds
NANOSECONDS : 86400000000000
MICROSECONDS : 86400000000
MILLISECONDS : 86400000
SECONDS : 86400
Thread  Execution Paused
Resuming Thread Execution...
MINUTES : 1440
HOURS : 24
DAYS : 1
Utility methods available in TimeUnit: 
 
simmytarika5
Java-Date-Time
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java