Java by API/java.util.concurrent/TimeUnit
Содержание
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));
}
}