java获得两个日期之间的所有月份

 1 private static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
 2     ArrayList<String> result = new ArrayList<String>();
 3     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
 4 
 5     Calendar min = Calendar.getInstance();
 6     Calendar max = Calendar.getInstance();
 7 
 8     min.setTime(sdf.parse(minDate));
 9     min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
10 
11     max.setTime(sdf.parse(maxDate));
12     max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
13 
14     Calendar curr = min;
15     while (curr.before(max)) {
16      result.add(sdf.format(curr.getTime()));
17      curr.add(Calendar.MONTH, 1);
18     }
19 
20     return result;
21   }

 

posted @ 2016-09-13 14:06  夏嘻嘻嘻嘻嘻  阅读(19886)  评论(0编辑  收藏  举报