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天