获取"yyyy-MM"格式月份集合
/**
* 获取指定月(包含指定月份)之前近n年月份集合
*
* @return
*/
public static List<String> agoMonthList(String inputMonth, int num) {
YearMonth yearMonth = YearMonth.parse(inputMonth, DateTimeFormatter.ofPattern("yyyy-MM"));
int startYear = yearMonth.getYear() - num;
int startMonth = yearMonth.getMonthValue();
int endYear = yearMonth.getYear();
int endMonth = yearMonth.getMonthValue();
List<String> monthList = new ArrayList<>();
for (int year = startYear; year <= endYear; year++) {
int currentStartMonth = (year == startYear) ? startMonth : 1;
int currentEndMonth = (year == endYear) ? endMonth : 12;
for (int month = currentStartMonth; month <= currentEndMonth; month++) {
LocalDate date = LocalDate.of(year, month, 1);
String formattedMonth = date.format(DateTimeFormatter.ofPattern("yyyy-MM"));
monthList.add(formattedMonth);
}
}
return monthList;
}