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

实现思路:

  通过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));

 

转载于:https://www.cnblogs.com/duwenlei/p/4272291.html

你可能感兴趣的:(计算两个时间段相差几个月(包含相差的哪些月份))