/**
* This Method is unit tested properly for very different cases , taking
* care of Leap Year days difference in a year, and date cases month and
* Year boundary cases (12/31/1980, 01/01/1980 etc)
*/
public static int getAge(Date dateOfBirth, Date toDate) {
if (dateOfBirth.after(toDate)) {
throw new IllegalArgumentException("the parameter dateOfBirth[" + dateOfBirth
+ "] can not be later than toDate[" + toDate + "]");
}
Calendar toDateCalendar = Calendar.getInstance();
Calendar birthCalendar = Calendar.getInstance();
birthCalendar.setTime(dateOfBirth);
toDateCalendar.setTime(toDate);
int age = toDateCalendar.get(Calendar.YEAR) - birthCalendar.get(Calendar.YEAR);
// If birth date is greater than todays date (after 2 days adjustment of
// leap year) then decrement age one year
if ((birthCalendar.get(Calendar.MONTH) > toDateCalendar.get(Calendar.MONTH))
|| (birthCalendar.get(Calendar.DAY_OF_YEAR)
- toDateCalendar.get(Calendar.DAY_OF_YEAR) > 3)) {
age--;
// If birth date and todays date are of same month and birth day of
// month is greater than todays day of month then decrement age
} else if ((birthCalendar.get(Calendar.MONTH) == toDateCalendar.get(Calendar.MONTH))
&& (birthCalendar.get(Calendar.DAY_OF_MONTH) > toDateCalendar
.get(Calendar.DAY_OF_MONTH))) {
age--;
}
return age;
}
public static int age(Date birthDate, Date toDate) {
if (birthDate.after(toDate)) {
throw new IllegalArgumentException("the parameter birthDate[" + birthDate
+ "] can not be later than toDate[" + toDate + "]");
}
Calendar cal = Calendar.getInstance();
Calendar now = Calendar.getInstance();
cal.setTime(birthDate);
now.setTime(toDate);
int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
|| (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) && cal
.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) {
res--;
}
return res;
}
分享到:
相关推荐
powerbuilder技术开发,根据转入生日,自动计算年龄。
delphi 通过日期计算年龄 ,tve 很不错的 delphi 代码 3
SQLServer 根据生日计算年龄
kettle中使用js计算年龄
sql 语句 根据身份证号计算年龄,oracl数据库可以直接运行
通过入参 生日 ,计算日期(默认sysdate),计算年龄 精确到秒
sql函数 可用于pb调用,根据生日日期,自动精确计算年龄。返回格式为xx岁xx月xx天 不受闰年影响
SQLSERVER计算年龄(岁月天),可以精确到岁,月,几月几天。
excel 计算年龄汇总,可以通过当前时间计算出一个人的年龄。
iOS计算年龄的封装
根据生日计算年龄JS
通过datepicker计算年龄
Java根据生日计算年龄算法 不满一年计算月份 不满一月计算天数
1、支持18、16位身份证号码,支持2、4、6、8位生日格式(例:88,1988,198804,...3、最小年龄和最大年龄用来设置输出年龄的范围。 4、计算日期默认为系统日期,可设置任意日期(例:2019年底须计算2020年体检单位日期)
在sqlserver中采用存储过程或函数计算年龄/周岁
excel中知道出生年月如何自动计算年龄
C#根据身份证号码计算年龄和性别
计算年龄的一个网页,采用了关机状态技术(全屏变黑,中间选择日期是亮的) 能计算你活了多长时间和你到下一次生日还需要多少时间!
TODAY()和DATE()函数计算年龄和工龄.xls
技巧:复制excel的身份证列CTRL+V到输入框后点查询,每次提交【6000】行以内,查询结果按行显示的。 功能:把身份证列的身份证复制过来可以查询身份证号的归属地、生日、年龄、性别等信息。