一、介绍
- 在JDK8中,可以使用java.time.Period来计算日期间隔差异
- 主要是Period类方法getYears(),getMonths()和getDays()来计算,只能精确到年月日
- 用于LocalDate之间的比较
二、实例
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate birthDate = LocalDate.of(2002,1,16);
System.out.println(birthDate);
Period period = Period.between(birthDate,today);
System.out.printf("年龄: %d 年 %d 月 %d 日",period.getYears(),period.getMonths(),period.getDays());
}