计算两个时间段相差几个月(包含相差的哪些月份)

实现思路:

  通过Calendar类的before来判断两个日期是否符合前一个日期小于后一个日期。

  实现:

 1         Calendar c1 = Calendar.getInstance();
 2         Calendar c2 = Calendar.getInstance();
 3         c1.set(2014, 9, 30);
 4         c2.setTime(new Date());
 5         System.out.println(c1.get(Calendar.YEAR)+" "+c1.get(Calendar.MONTH));
 6         while (c1.before(c2)) {
 7             int m = c1.get(Calendar.MONTH)+1;
 8             System.out.println(c1.get(Calendar.YEAR)+" "+(c1.get(Calendar.MONTH)+1));
 9             c1.add(Calendar.MONTH,1);
10         }
11         System.out.println(c1.get(Calendar.YEAR)+" "+(c1.get(Calendar.MONTH)+1));

 

posted @ 2015-02-04 14:33  哎呦喂,我的小祖宗╰つ  阅读(1536)  评论(0编辑  收藏  举报