计算两个String 类型的时间相关几个月

/**
	 * 返回两个时间段相隔几个月
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException 
	 * @throws ParseException
	 */
	public  static  long getMonth(String startDate, String endDate) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        long monthday;
        Calendar starCal = Calendar.getInstance();
        starCal.setTime(f.parse(startDate));

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(f.parse(endDate));

        monthday = ((endCal.get(Calendar.YEAR) - starCal.get(Calendar.YEAR)) * 12 + (endCal.get(Calendar.MONTH) - starCal.get(Calendar.MONTH)));

        if (starCal.get(Calendar.DATE) < endCal.get(Calendar.DATE)) {
            monthday = monthday + 1;
        }
        return monthday;
    }

原文地址:https://www.cnblogs.com/jzssuanfa/p/6979530.html