1.同月相差天数,月份,年份
LocalDate start = LocalDate.of(2021,5,1);LocalDate end = LocalDate.now();Period next = Period.between(start,end);next.getDays();//相差天数next.getMonths();//相差月份next.getYears();//相差年份
2.只需要获取天数差
2.1 请看LocalDate.toEpochDay()源码
@Overridepublic long toEpochDay() {long y = year;long m = month;long total = 0;total += 365 * y;if (y >= 0) {total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400;} else {total -= y / -4 - y / -100 + y / -400;}total += ((367 * m - 362) / 12);total += day - 1;if (m > 2) {total--;if (isLeapYear() == false) {total--;}}return total - DAYS_0000_TO_1970;}
2.2 具体使用
LocalDate start = LocalDate.of(2021,5,1);//开始时间LocalDate end = LocalDate.now();//当前时间Long cha = end.toEpochDay() - start.toEpochDay();//天数差