Friday, 7 March 2014

convert : Time unit conversion

public long convert(long sourceDuration, TimeUnit sourceUnit)
Convert the given time duration in the given unit to this unit.

import java.util.concurrent.TimeUnit;
public class TimeUnitEx {
 public static void main(String args[]){
  System.out.print(" 1 Day = ");
  System.out.println(TimeUnit.HOURS.convert(1, TimeUnit.DAYS) + " Hours");

  System.out.print(" 24 Hours = ");
  System.out.println(TimeUnit.MINUTES.convert(24, TimeUnit.HOURS) + " Minutes");

  System.out.print(" 1440 Minutes = ");
  System.out.println(TimeUnit.SECONDS.convert(1440, TimeUnit.MINUTES) + " Seconds");

  System.out.print(" 86400 Seconds = ");
  System.out.println(TimeUnit.MILLISECONDS.convert(86400, TimeUnit.SECONDS) + " MilliSeconds");

  System.out.print(" 86400000 MilliSeconds = ");
  System.out.println(TimeUnit.MICROSECONDS.convert(86400000, TimeUnit.MILLISECONDS) + " MicroSeconds");

  System.out.print(" 86400000000 MicroSeconds = ");
  System.out.println(TimeUnit.NANOSECONDS.convert(86400000000L, TimeUnit.MICROSECONDS) + " NanoSecnds");
 }
}

Output   
1 Day = 24 Hours
24 Hours = 1440 Minutes
1440 Minutes = 86400 Seconds
86400 Seconds = 86400000 MilliSeconds
86400000 MilliSeconds = 86400000000 MicroSeconds
86400000000 MicroSeconds = 86400000000000 NanoSecnds


As you observe the below statement
    TimeUnit.HOURS.convert(1, TimeUnit.DAYS)
Converts one day into hours.

Related Posts


Get TimeUnit constants                                                 TimeUnit Conversioin methods                                                 Home

No comments:

Post a Comment