2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Java Enum TimeUnit with the Enum constants and method implementations

Overview

Being a programmer so many of scenarios we may came across in our development life cycle where the implementation logic needs a time conversion i.e, converting between units of time into days,hours,minutes and so on.To overcome this type of scenarios we may write our own utility methods or making use of the 3rd party dependencies available in market.On the same way java API also having one of the most useful and important Enum i.e TimeUnit which belongs to the package java.util.concurrent since jdk 1.5 on wards. Although it is part of the java.util.concurrent package, the TimeUnit enum is useful in many contexts outside of concurrency too.


Java API Specification

"A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units. A TimeUnit does not maintain time information, but only helps organize and use time representations that may be maintained separately across various contexts. A nanosecond is defined as one thousandth of a microsecond, a microsecond as one thousandth of a millisecond, a millisecond as one thousandth of a second, a minute as sixty seconds, an hour as sixty minutes, and a day as twenty four hours."


Means, the TimeUnit enum is useful for converting between units of time in a highly readable and standardized approach.


TimeUnit Enum Constants

DAYS 

HOURS 

MICROSECONDS 

MILLISECONDS 

MINUTES 

NANOSECONDS 

SECONDS 


TimeUnit Conversion Methods

long convert(long sourceDuration, TimeUnit sourceUnit)

long toDays(long duration)

long toHours(long duration)

long toMicros(long duration)

long toMillis(long duration)

long toMinutes(long duration)

long toNanos(long duration)

long toSeconds(long duration)


Example of TimeUnit


import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TestTimeUnit {
	public static void main(String[] args) {
		// Current date in millisecond
		long toMiliseconds = new Date().getTime();
		System.out.println("Current date in milliseconds : " + toMiliseconds);

		// 1. long toMillis(long duration)
		long fromMiliSeconds = new Date(new Date().getTime() - TimeUnit.DAYS.toMillis(2)).getTime();
		System.out.println("Decrement date by days in milliseconds : " + fromMiliSeconds);

		// find out the duration between the dates in milliseconds
		long durationMilliSeconds = toMiliseconds - fromMiliSeconds;

		// 2. long toNanos(long duration)
		long toNanos = TimeUnit.MILLISECONDS.toNanos(durationMilliSeconds);
		System.out.println("Convert milliseconds in nanos : " + toNanos);

		// 3. long toSeconds(long duration)
		long toSeconds = TimeUnit.MILLISECONDS.toSeconds(durationMilliSeconds);
		System.out.println("Convert milliseconds in seconds : " + toSeconds);

		// 4. long toMinutes(long duration)
		long toMinuts = TimeUnit.MILLISECONDS.toMinutes(durationMilliSeconds);
		System.out.println("Convert milliseconds in minutes : " + toMinuts);

		// 5. long toHours(long duration)
		long toHours = TimeUnit.MILLISECONDS.toHours(durationMilliSeconds);
		System.out.println("Convert milliseconds in hours : " + toHours);

		// 6. long toDays(long duration)
		long toDays = TimeUnit.MILLISECONDS.toDays(durationMilliSeconds);
		System.out.println("Convert milliseconds in days : " + toDays);

		// 7. long convert(long sourceDuration, TimeUnit sourceUnit)
		// Convert the given time duration in the given unit to specified unit.
		// For example to convert milliseconds in days
		long milliToDays = TimeUnit.DAYS.convert(durationMilliSeconds, TimeUnit.MILLISECONDS);
		System.out.println("Convert milliseconds in days using convert method : " + milliToDays);

		/*
		 * similarly we can make use of others Enum constants as part of time
		 * conversion i.e. from days to minutes and so on
		 */
		long daysToMinuts = TimeUnit.DAYS.toMinutes(toDays);
		System.out.println("Convert days in minuts : " + daysToMinuts);

	}
}
Results :

Current date in milliseconds : 1481283698496
Decrement date by days in milliseconds : 1481110898496
Convert milliseconds in nanos : 172800000000000
Convert milliseconds in seconds : 172800
Convert milliseconds in minutes : 2880
Convert milliseconds in hours : 48
Convert milliseconds in days : 2
Convert milliseconds in days using convert method : 2
Convert days in minuts : 2880


Conclusion

Though now a days we have other alternates also available for the date conversion like Java 8 LocalDate or one of the 3rd party dependency JodaTime etc. TimeUnit is also one of the best approach to converting units of time in a highly readable and standardized format. And in this blog I provided a best sweetable example which will helpful for the programmers to develop the utility class or internal time conversion library by referring to it.