java 计算两个日期相差了几个月

/**
 * 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

转载只是防止作者误删!

    原文作者:小崔_01
    原文地址: https://blog.csdn.net/qq_33887333/article/details/72676492
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞