/**
* 判断时间段之间的月份 (yyyy-MM)
* @param minDate
* @param maxDate
* @return
*/
public static Set<Integer> getMonthBetween(String minDate, String maxDate){
Set<Integer> result = new HashSet<>();
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
try{
min.setTime(strDate(minDate, "yyyy-MM"));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(strDate(maxDate, "yyyy-MM"));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
}catch (Exception e){
throw new ServiceException("分解月份出错");
}
Calendar curr = min;
while (curr.before(max)) {
result.add(curr.get(Calendar.MONTH) + 1);
curr.add(Calendar.MONTH, 1);
}
min = null;max = null;curr = null;
return result;
}
测试