java两个时间段之间取每个年月

来源:互联网 发布:section软件最新版 编辑:程序博客网 时间:2024/06/10 00:35
  1. /** 
  2.      *  
  3.      * @param minDate 最小时间  2015-01 
  4.      * @param maxDate 最大时间 2015-10 
  5.      * @return 日期集合 格式为 年-月 
  6.      * @throws Exception 
  7.      */  
  8.     public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception {  
  9.         ArrayList<String> result = new ArrayList<String>();  
  10.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月  
  11.   
  12.         Calendar min = Calendar.getInstance();  
  13.         Calendar max = Calendar.getInstance();  
  14.   
  15.         min.setTime(sdf.parse(minDate));  
  16.         min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);  
  17.   
  18.         max.setTime(sdf.parse(maxDate));  
  19.         max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);  
  20.   
  21.         Calendar curr = min;  
  22.         while (curr.before(max)) {  
  23.          result.add(sdf.format(curr.getTime()));  
  24.          curr.add(Calendar.MONTH, 1);  
  25.         }  
  26.   
  27.         return result;  
  28.     }  
  29. 返回的集合就是每个年-月了
原创粉丝点击