`

Calculate Days Between Two Date

    博客分类:
  • Java
 
阅读更多

计算 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);
}

 

分享到:
评论

相关推荐

    二级减速器课程设计说明书reducer design specification.doc

    Working hours per day: 16 hours, working life: 10 years, working days per year: 300 days, equipped with three-phase AC power supply, voltage 380/220 V. 1.3 Design Data Working force of conveyor, F ...

    计算机网络第六版答案

    Traffic between the Google data centers passes over its private network rather than over the public Internet. Many of these data centers are located in, or close to, lower tier ISPs. Therefore, when ...

    SSD7 选择题。Multiple-Choice

    For two tables to be union compatible, the tables should be the same with respect to which of the following? (a) keys (b) cardinality (c) name (d) degree Correct answer is (d) Your ...

    Linux高级bash编程

    使用 date 命令 12-11. 分析单词出现的频率 12-12. 那个文件是脚本? 12-13. 产生10进制随机数 12-14. 使用 tail 命令来监控系统log 12-15. 在一个脚本中模仿 "grep" 的行为 12-16. 在1913年的韦氏词典中查找定义 12...

    Advanced Bash-Scripting Guide <>

    使用 date 命令 12-11. 分析单词出现的频率 12-12. 那个文件是脚本? 12-13. 产生10 进制随机数 12-14. 使用 tail 命令来监控系统log 12-15. 在一个脚本中模仿 "grep" 的行为 12-16. 在1913 年的韦氏词典中查找定义 ...

Global site tag (gtag.js) - Google Analytics