TimeUnit Class in Java with Examples
Last Updated :
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 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
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String args[])
{
long hours = 96 ;
long days = TimeUnit.DAYS.convert(hours, TimeUnit.HOURS);
long minutes = TimeUnit.MINUTES.convert(days, TimeUnit.DAYS);
System.out.println(hours + " Hours = " + days
+ " Days = " + +minutes
+ " Minutes" );
long micros = TimeUnit.MINUTES.toMicros(minutes);
System.out.println(minutes + " Minutes = " + micros
+ " Microseconds" );
long seconds = TimeUnit.MICROSECONDS.toSeconds(micros);
System.out.println(micros + " Microseconds = "
+ seconds + " Seconds" );
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
import java.util.concurrent.TimeUnit;
public class GFG implements Runnable {
public void run()
{
for (TimeUnit unit : TimeUnit.values()) {
try {
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" );
Thread t1 = new Thread(obj1);
t1.start();
System.out.println( "Now, thread will run for 5 seconds" );
try {
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. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...