1 例子
package com.javaTechnology;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Set;
public class TimeTest {
public static void main(String[] args) {
test(10);
t1();
t2();
t3();
t4();
}
private static void t4() {
ZonedDateTime now = ZonedDateTime.now();
System.out.println("输出当前时间包括时区:"+now);
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
System.out.println("输出所有时区:"+availableZoneIds);
ZonedDateTime of = ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("Asia/Shanghai"));
System.out.println("输出时间包括时区:"+of);
Instant instant = of.toInstant();
System.out.println("转换为默认时区:"+instant);
}
private static void t3() {
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate alonzosBirthday = LocalDate.of(1983, Month.JUNE, 14);
System.out.println(alonzosBirthday);
long until = alonzosBirthday.until(today, ChronoUnit.DAYS);
System.out.println("到现在经过了多少天:"+until);
DayOfWeek dayOfWeek = today.getDayOfWeek();
System.out.println("今天星期几:"+dayOfWeek.getValue());
System.out.println("星期六的3天后是星期几:"+DayOfWeek.SATURDAY.plus(3).getValue());
LocalDate programmersDay = today.plusDays(255);
System.out.println("得到255天后的时间:"+programmersDay);
LocalTime now = LocalTime.now();
System.out.println("获取当前时间:"+now);
}
private static void t2() {
Duration test = test(90);
Duration test1 = test(1000);
boolean overTheTimesFaster = test.multipliedBy(10).minus(test1).isNegative();
System.out.println(overTheTimesFaster);
boolean overTheTimesFaster1 = test.toNanos() * 10 < test1.toNanos();
System.out.println(overTheTimesFaster1);
}
private static void t1() {
Instant start = Instant.now();
Instant later = start.plus(Duration.ofHours(8));
System.out.println(start);
System.out.println(later);
}
private static Duration test(int number) {
Instant start = Instant.now();
runAlgorithm(number);
Instant end = Instant.now();
Duration timeElapsed = Duration.between(start, end);
long millis = timeElapsed.toMillis();
System.out.println("计算过去了多少时间毫秒:"+millis);
return timeElapsed;
}
private static void runAlgorithm(int number) {
try {
Thread.sleep(number);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}