public static int getMonthDiff2(String startDateStr, String endDateStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
YearMonth startDate = YearMonth.parse(startDateStr, formatter);
YearMonth endDate = YearMonth.parse(endDateStr, formatter);
int startMonthNumber = startDate.getYear() * 12 + startDate.getMonthValue();
int endMonthNumber = endDate.getYear() * 12 + endDate.getMonthValue();
return endMonthNumber - startMonthNumber;
}
//调用该方法
System.out.println("计算二个月份之间的差值"+getMonthDiff2("202204","202304"));
传入参数类型为202305的String类型,求二个月份之间的差值
于 2023-05-25 21:16:21 首次发布