写代码过程中有一个需求是给一个出生日期,要计算出生日期和当前日期之间的年月日,发现可以用
Period
这个类实现
//出生日期
LocalDate birth = LocalDate.of(2000, 11, 3);
Period period = Period.between(birth, LocalDate.now());
//两个日期间的年份
System.err.println(period.getYears());
//两个日期间的月份
System.err.println(period.getMonths());
//两个日期间的天份
System.err.println(period.getDays());
效果: