1. 概述
- 从Java8开始,java.time包提供了新的日期和时间API,主要涉及的类型有:
- 新增的API严格区分了时刻、本地日期、本地时间,并且,对日期和时间进行运算更加方便。
- 其次,新API的类型几乎全部是不变类型(和String的使用类似),可以放心使用不必担心被修改。
2. LocalDate、LocalTime、LocalDateTime
2.1 概述
- 它们分别表示日期,时间,日期时间对象,它们的类的实例是不可变的对象。
- 它们三者构建对象和API都是通用的
构建对象的方式如下:
2.2 LocalLocalDate、LocalTime、LocalDateTime获取信息的API
2.3 转换相关的API
2.4 修改相关的API
3. Instant
- JDK8获取时间戳特别简单,且功能丰富。Instant类由一个静态的工厂方法now()可以返回当前时间戳。
- 时间戳是包含日期和时间的,与java.util.Date很类似,事实上Instant就是类似JDK8以前的Date。
- Instant和Date这两个类可以进行转换。
4. DateTimeFormatter
- 在JDK8中,引入了一个全新的日期与时间各时期DateTimeFormatter。
- 正反都能调用format方法
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Demo06DateTimeFormat {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a");
System.out.println(dtf.format(ldt));
System.out.println(ldt.format(dtf));
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt1 = LocalDateTime.parse("2019-11-11 11:11:11" , dtf1);
System.out.println(ldt1);
System.out.println(ldt1.getDayOfYear());
}
}
5. Duration/Period
5.1 Period
- 在JDK8中,我们可以使用以下类来计算日期间隔差异:java.time.Period
- 主要是Period类方法getYears(),getMonths(),getDays()来计算,只能精确到年月日。
- 用于LocalDate之间的比较。
import java.time.LocalDate;
import java.time.Period;
public class Demo07Period {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate birthDate = LocalDate.of(1998, 6, 1);
System.out.println(birthDate);
Period period = Period.between(birthDate, today);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
}
5.2 Duration
- 在JDK8中,我们可以使用以下类来计算日期间隔差异:java.time.Duration
- 提供了基于时间的值测量时间量的方法。
- 用于LocalDateTime之间的比较。也可以用于Instant之间的比较。
import java.time.Duration;
import java.time.LocalDateTime;
public class Demo08Duration {
public static void main(String[] args) {
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthDate = LocalDateTime.of(2021,8
,06,01,00,00);
System.out.println(birthDate);
Duration duration = Duration.between( birthDate , today);
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.toMillis());
System.out.println(duration.toNanos());
}
}
6. ChronoUnit
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class Demo09ChronoUnit {
public static void main(String[] args) {
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthDate = LocalDateTime.of(1998,6,1,
10,50,59);
System.out.println(birthDate);
System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));
System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));
System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today));
System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today));
System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today));
System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today));
System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today));
System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today));
System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today));
System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today));
System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today));
System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today));
System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today));
System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));
}
}