/**
* 获取两个日期之间的月数间隔
* @param begin
* @param end
* @param abs
* @return
*/
public static int getTwoDatesDifMonth(Date begin, Date end, boolean abs) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(begin);
c2.setTime(end);
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);
int month1 = c1.get(Calendar.MONTH);
int month2 = c2.get(Calendar.MONTH);
int day1 = c1.get(Calendar.DAY_OF_MONTH);
int day2 = c2.get(Calendar.DAY_OF_MONTH);
// 获取年的差值
int yearInterval = year1 - year2;
// 获取月数差值
int monthInterval = month1 - month2;
if (day1 <= day2) {
monthInterval--;
}
int monthsDiff = yearInterval * 12 + monthInterval;
return abs? Math.abs(monthsDiff) : monthsDiff;
}
获取两个日期的月数间隔
最新推荐文章于 2024-04-13 15:37:12 发布