/**
* 返回两个时间段相隔几个月
* @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;
}
计算两个String 类型的时间相关几个月
最新推荐文章于 2022-09-27 16:24:28 发布