計算兩個日期相差幾個月

public static int getMonthsBetweenDays(Date beginDate, Date endDate) {
		Calendar c = new GregorianCalendar();
		c.setTime(beginDate);
		int beginYear = c.get(Calendar.YEAR);
		int beginMonth = c.get(Calendar.MONDAY) + 1;
		c.setTime(endDate);
		int endYear = c.get(Calendar.YEAR);
		int endMonth = c.get(Calendar.MONDAY) + 1;
		// 獲取相差的月份
		return (endYear - beginYear - 1) * 12 + (12 - beginMonth) + 1 + endMonth;
	}
相關文章
相關標籤/搜索