DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDate startDate = LocalDate.parse(startTime, formatter); LocalDate endDate = LocalDate.parse(endTime, formatter); // 日期区间 long days = ChronoUnit.DAYS.between(startDate, endDate); //月 long month = ChronoUnit.MONTHS.between(startDate, endDate); //年 long year = ChronoUnit.YEARS.between(startDate, endDate);
更多用法见官网