Searching...
Tuesday, 27 May 2014

java.util.concurrent.TimeUnit Example

09:41

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. Here is one example to do the following

Days to Hours

Hours to Minutes

Minutes to Seconds

Seconds to Milliseconds

Milliseconds to Micro seconds

Example

package com.pretech;
import java.util.concurrent.TimeUnit;
public class TimeUtilTest {
	public static void main(String[] args) {
		try {
			// Days to hours
			System.out.println("Total Hours" + TimeUnit.DAYS.toHours(1));
			// Hours to minutes
			System.out.println("Total Minutes" + TimeUnit.HOURS.toMinutes(24));
			// Minutes to Seconds
			System.out.println("Total Seconds"
					+ TimeUnit.MINUTES.toSeconds(1440));
			// Seconds to Mill seconds
			System.out.println("Total Milli seconds"
					+ TimeUnit.SECONDS.toMillis(86400));
			// Milli seconds to micro seconds
			System.out.println("Total Micro seconds "
					+ TimeUnit.MILLISECONDS.toMicros(86400000));
			// TimeUnit to sleep
			System.out.println("Before sleep ");
			TimeUnit.SECONDS.sleep(5);
			System.out.println("After sleep ");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output



Total Hours24
Total Minutes1440
Total Seconds86400
Total Milli seconds86400000
Total Micro seconds 86400000000
Before sleep
After sleep


0 comments: