Calendar cal = Calendar.getInstance();
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;
}else{
age--;
}
}
return age;
}
jquery 根据出生年月日得到年龄
function byage(strBirthday ){
var returnAge;
var strBirthdayArr=strBirthday.split("-");
var birthYear = strBirthdayArr[0];
var birthMonth = strBirthdayArr[1];
var birthDay = strBirthdayArr[2];
d = new Date();
var nowYear = d.getFullYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
if(nowYear == birthYear){
returnAge = 0;//同年 则为0岁
}
else{
var ageDiff = nowYear - birthYear ; //年之差
if(ageDiff > 0){
if(nowMonth == birthMonth) {
var dayDiff = nowDay - birthDay;//日之差
if(dayDiff < 0)
{
returnAge = ageDiff - 1;
}
else
{
returnAge = ageDiff ;
}
}
else
{
var monthDiff = nowMonth - birthMonth;//月之差
if(monthDiff < 0)
{
returnAge = ageDiff - 1;
}
else
{
returnAge = ageDiff ;
}
}
}
else
{
returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
}
}
return returnAge;//返回周岁年龄
}
JAVA 根据出生年月日得到年龄 private int getAge(Date birthDay){ Calendar cal = Calendar.getInstance(); int yearNow = cal.get(Calendar.YEAR); int monthNow = cal.get(Calendar.MONTH)...