Java by API/java.util.concurrent/TimeUnit — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:23, 31 мая 2010

TimeUnit: DAYS

  
import java.util.concurrent.TimeUnit;
public class Main {
  public static void main(String[] args) {
    TimeUnit tu = TimeUnit.DAYS;
    System.out.println(tu.toDays(1));
    System.out.println(tu.toHours(1));
    System.out.println(tu.toMinutes(1));
  }
}





TimeUnit.MILLISECONDS

 

import java.util.Calendar;
import java.util.concurrent.TimeUnit;
public class Main {
  public static long getDifference(Calendar a, Calendar b, TimeUnit units) {
    return units.convert(b.getTimeInMillis() - a.getTimeInMillis(), TimeUnit.MILLISECONDS);
  }
  public static void main(String[] args) {
    Calendar first = Calendar.getInstance();
    first.set(2008, Calendar.AUGUST, 1);
    Calendar second = Calendar.getInstance();
    System.out.println(getDifference(first, second, TimeUnit.DAYS) + " day(s) between ");
  }
}





TimeUnit.SECONDS

  
import static java.util.concurrent.TimeUnit.SECONDS;
public class MainClass extends Thread {
  // This field is volatile because two different threads may access it
  volatile boolean keepRunning = true;
  public MainClass() {
    setDaemon(true);
  }
  public void run() {
    while (keepRunning) {
      long now = System.currentTimeMillis();
      System.out.printf("%tr%n", now);
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        return;
      }
    }
  }
  public void pleaseStop() {
    keepRunning = false;
  }
  public static void main(String[] args) {
    MainClass thread = new MainClass();
    thread.start();
    try {
      SECONDS.sleep(10);
    } catch (InterruptedException ignore) {
    }
    thread.pleaseStop();
  }
}





TimeUnit: sleep(long timeout)

  
import static java.util.concurrent.TimeUnit.SECONDS;
public class MainClass extends Thread {
  // This field is volatile because two different threads may access it
  volatile boolean keepRunning = true;
  public MainClass() {
    setDaemon(true);
  }
  public void run() {
    while (keepRunning) {
      long now = System.currentTimeMillis();
      System.out.printf("%tr%n", now);
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        return;
      }
    }
  }
  public void pleaseStop() {
    keepRunning = false;
  }
  public static void main(String[] args) {
    MainClass thread = new MainClass();
    thread.start();
    try {
      SECONDS.sleep(10);
    } catch (InterruptedException ignore) {
    }
    thread.pleaseStop();
  }
}





TimeUnit: toDays(long duration)

  
import java.util.concurrent.TimeUnit;
public class Main {
  public static void main(String[] args) {
    TimeUnit tu = TimeUnit.DAYS;
    System.out.println(tu.toDays(1));
    System.out.println(tu.toHours(1));
    System.out.println(tu.toMinutes(1));
  }
}





TimeUnit: toHours(long duration)

  
import java.util.concurrent.TimeUnit;
public class Main {
  public static void main(String[] args) {
    TimeUnit tu = TimeUnit.DAYS;
    System.out.println(tu.toDays(1));
    System.out.println(tu.toHours(1));
    System.out.println(tu.toMinutes(1));
  }
}





TimeUnit: toMinutes(long duration)

  
import java.util.concurrent.TimeUnit;
public class Main {
  public static void main(String[] args) {
    TimeUnit tu = TimeUnit.DAYS;
    System.out.println(tu.toDays(1));
    System.out.println(tu.toHours(1));
    System.out.println(tu.toMinutes(1));
  }
}