/** * 返回两个时间段相隔几个月 * @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; }