您的位置:首页 > 其它

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

2017-06-10 18:32 274 查看
/**
* 返回两个时间段相隔几个月
* @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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: