1.获取日期集合中最大连续月份的个数
/**
* 获取日期集合中最大连续月份的个数(用于计算最大连续逾期期数)
*
* @param dateList
* @return
* @throws ParseException
*/
public int getMaxConsecutiveMonths(List<String> dateList) throws ParseException {
// 将日期字符串转换为Date对象,并排序日期集合
List<Date> sortedDates = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (String dateString : dateList) {
Date date = sdf.parse(dateString);
sortedDates.add(date);
}
Collections.sort(sortedDates);
// 计算最大连续月份个数
int maxConsecutiveMonths = 1;
int currentConsecutiveMonths = 1;
for (int i = 1; i < sortedDates.size(); i++) {
// 判断相邻两个日期是否是连续的月份
long diffInMonths = monthsBetween(sortedDates.get(i - 1), sortedDates.get(i));
if (diffInMonths == 1) {
currentConsecutiveMonths++;
if (currentConsecutiveMonths > maxConsecutiveMonths) {
maxConsecutiveMonths = currentConsecutiveMonths;
}
} else {
currentConsecutiveMonths = 1;
}
}
return maxConsecutiveMonths;
}
2.获取两个年月之间的所有月份,并进行倒序排序
/**
* 获取两个年月之间的所有月份,并进行倒序排序
*
* @param startDate
* @param endDate
* @return
*/
private static List<String> calculateYearMonths(String startDate, String endDate) {
List<String> yearMonths = new ArrayList<>();
YearMonth startYearMonth = YearMonth.parse(startDate);
YearMonth endYearMonth = YearMonth.parse(endDate);
YearMonth currentYearMonth = startYearMonth;
while (!currentYearMonth.isAfter(endYearMonth)) {
yearMonths.add(currentYearMonth.toString());
currentYearMonth = currentYearMonth.plusMonths(1);
}
Collections.reverse(yearMonths);
return yearMonths;
}