/**
* java 计算两个日期相差了几个月
*/
public class MonthTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String str1 = "2012-02";
String str2 = "2010-01";
Calendar bef = Calendar.getInstance();
Calendar aft = Calendar.getInstance();
bef.setTime(sdf.parse(str1));
aft.setTime(sdf.parse(str2));
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
System.out.println(Math.abs(month + result));
}
}
原文来自:http://blog.csdn.net/u012184853/article/details/51144194
转载只是防止作者误删!