Skip to content
Related Articles

Related Articles

Improve Article

TimeUnit Class in Java with Examples

  • Last Updated : 02 Feb, 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:

  1. Nanoseconds: One thousandth of a microsecond
  2. Microseconds: One thousandth of a millisecond
  3. Milliseconds: One thousandth of a second
  4. Seconds: Oone second
  5. Minutes: Sixty seconds
  6. Hours: Sixty minutes
  7. 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);
    }
}
Output
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...");
    }
}
Output
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:

Methods                                                                   Description
long convert()Converts the time duration inputted with its unit to the required unit.
long toDays() Converts the time duration to Days.
long toHours() Converts the time duration to Hours.
long toMicros()Converts the time duration to Microseconds.
long toMillis() Converts the time duration to Milliseconds.
long toMinutes() Converts the time duration to Minutes.
long toNanos()Converts the time duration to Nanoseconds.
long toSeconds() Converts the time duration to Seconds.
static TimeUnit valueOf()Returns the enum constant of the type with the specified name.
static TimeUnit [] values()Returns an array containing the enum constants.
void sleep()Performs a Thread.sleep using this time unit. Pause for given TimeUnit.
void timedWait() Performs a timed Object.wait using this time unit. Wait for the given time unit to execute.
void timedJoin()Performs a timed Thread.join using this time unit. Thread is provided to do work for a given time duration only.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :