2个时间差,大于1年显示no,小于等于1年显示yes public static void main(String[] args) { // String date1 = "2023-03-01"; // String date2 = "2024-03-01"; String date1 = "2023-02-28"; String date2 = "2024-02-29"; // 将字符串解析为LocalDate对象 LocalDate localDate1 = LocalDate.parse(date1); LocalDate localDate2 = LocalDate.parse(date2); // 计算日期差 Period period = Period.between(localDate1, localDate2); System.out.println(period.getYears()); System.out.println(period.getMonths()); System.out.println(period.getDays()); // 将年、月、日转换为总的天数 int totalDays = period.getYears() * 365 + period.getMonths() * 30 + period.getDays(); System.out.println("totalDays"+totalDays); // 比较总天数是否大于365天 boolean isGreaterThanOneYear = totalDays > 365; // 根据比较结果显示yes或no if (isGreaterThanOneYear) { System.out.println("yes"); } else { System.out.println("no"); } // 计算日期之间的相差天数 long daysDifference = ChronoUnit.DAYS.between(localDate1, localDate2); System.out.println("daysDifference"+daysDifference); // String date1 = "2023-03-01"; // String date2 = "2024-03-01"; // no // String date1 = "2023-02-28"; // String date2 = "2024-02-29"; // yes }