LocalDate提供了until()方法,用于计算了两个日期之间的年、月和日的周期;
举个栗子:计算“2017-08-28”“2018-09-30”两日期间间隔的几年几个月零几天
LocalDate localDate1 = LocalDate.parse("2017-08-28");
LocalDate LocalDate2 = LocalDate.parse("2018-09-30");
int years = localDate1.until(LocalDate2).getYears();
int months = localDate1.until(LocalDate2).getMonths();
int days = localDate1.until(LocalDate2).getDays();
System.out.println("间隔:"+years + " years," + months + " months and " + days + " days");
输出结果:
间隔:1 years, 1 months and 2 days
LocalDate提供了until(Temporal endExclusive, TemporalUnit unit),计算两个日期间的间隔
LocalDate start = LocalDate.parse("2018-08-28");
LocalDate end = LocalDate.parse("2018-09-30");
long year = start.until(end, ChronoUnit.YEARS);
long month = start.until(end, ChronoUnit.MONTHS);
long days = start.until(end, ChronoUnit.DAYS);
System.out.println("间隔:" + year + "年");
System.out.println("间隔:" + month + "月");
System.out.println("间隔:" + days + "天");
计算结果:
间隔:0年
间隔:1月
间隔:33天