Calculate Days Between Two Date

计算 2 个日期间相差的天数

 

 

/**
 * Calculate Days between two Date
 * @param isOrderless	DEFAULT true
 * @param dateEarlier	date
 * @param dateLater		another date
 * @return
 */
public static int getDaysBetween(Boolean isOrderless, Calendar dateEarlier, Calendar dateLater) {
	// 排序日期
	if (isOrderless) { 
		if (dateEarlier.after(dateLater)) {
			Calendar swap = dateLater;
			dateLater = dateEarlier;
			dateEarlier = swap;
			isOrderless	= false;
		}
		isOrderless	= false;
	}
	// 如果同年, 则直接给出天数差
	if (dateLater.get(Calendar.YEAR) == dateEarlier.get(Calendar.YEAR))
		return dateLater.get(Calendar.DAY_OF_YEAR)
				- dateEarlier.get(Calendar.DAY_OF_YEAR) + 1;
	// 否则, 加上间隔年的天数并进入迭代
	dateEarlier.add(Calendar.YEAR, 1);
	return dateEarlier.getMaximum(Calendar.DAY_OF_YEAR) + getDaysBetween(isOrderless, dateEarlier, dateLater);
}

 

你可能感兴趣的:(Date)