Java 根据出生日期获得年龄

数据库中有一个字段 出生日期
我要写一个方法,目的就是把出生日期传进去,然后返回年龄,要算周岁
精确到日啊
怎么写?请大侠帮帮忙了,谢谢!
就是不可以算虚岁,不能用现在的年减去出生的年这么简单来算

实现步骤:

1、获取当前时间

2、判断出生日期是否小于当前时间,如果大于,则引发一场

3、从当前时间中取出年、月、日;从出生日期中取出年、月、日,年份相减

4、然后做具体判断

示例代码如下:

public static int getAge(Date birthDay) throws Exception { 
        //获取当前系统时间
        Calendar cal = Calendar.getInstance(); 
        //如果出生日期大于当前时间,则抛出异常
        if (cal.before(birthDay)) { 
            throw new IllegalArgumentException( 
                "The birthDay is before Now.It's unbelievable!"); 
        } 
        //取出系统当前时间的年、月、日部分
        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; 
        //当前月份与出生日期的月份相比,如果月份小于出生月份,则年龄上减1,表示不满多少周岁
        if (monthNow <= monthBirth) { 
            //如果月份相等,在比较日期,如果当前日,小于出生日,也减1,表示不满多少周岁
            if (monthNow == monthBirth) { 
                if (dayOfMonthNow < dayOfMonthBirth) age--; 
            }else{ 
                age--; 
            } 
        } 
        System.out.println("age:"+age); 
        return age; 
    }
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-09
String t1 = 现在实际日期.replace('-','/');
String t2 = 出生日期.replace('-','/');

Date dt1= new Date(t1);
Date dt2= new Date(t2);
long i= (dt1.getTime() - dt2.getTime())/(1000*60*60*24);
//i就是总天数了,之后你除个365 就是岁数, 余数就是天数 要算月的话就把余数再除以12 我的这个日期的格式是2008-11-22
实岁就是现在的年减去出生的年呀,虚岁是实岁再加1 这个天数就是能比较精确了吧本回答被提问者采纳
第2个回答  推荐于2017-10-15
简单实现代码如下:

public static int getAge(Date birthDate) {

if (birthDate == null)
throw new
RuntimeException("出生日期不能为null");

int age = 0;

Date now = new Date();

SimpleDateFormat format_y = new
SimpleDateFormat("yyyy");
SimpleDateFormat format_M = new
SimpleDateFormat("MM");

String birth_year =
format_y.format(birthDate);
String this_year =
format_y.format(now);

String birth_month =
format_M.format(birthDate);
String this_month =
format_M.format(now);

// 初步,估算
age =
Integer.parseInt(this_year) - Integer.parseInt(birth_year);

// 如果未到出生月份,则age - 1
if
(this_month.compareTo(birth_month) < 0)
age -=
1;
if (age <
0)
age =
0;
return age;
}
第3个回答  2019-05-07
private static String dealAge(int days, Date bornDate){
String age = "";
if(days < 0 || bornDate == null){
return "-";
}
Date currentDate = DateTool.getNextDate(bornDate, days); //当前日期

Calendar from = Calendar.getInstance();
from.setTime(bornDate);
Calendar to = Calendar.getInstance();
to.setTime(currentDate);

int fromYear = from.get(Calendar.YEAR);
int fromMonth = from.get(Calendar.MONTH);
int fromDay = from.get(Calendar.DAY_OF_MONTH);
int toYear = to.get(Calendar.YEAR);
int toMonth = to.get(Calendar.MONTH);
int toDay = to.get(Calendar.DAY_OF_MONTH);
int year = toYear - fromYear;
int month = toMonth - fromMonth;
int day = toDay - fromDay;

if(month < 0){
year += -1;
month += 12;
}
if(day < 0 ){
month += -1;
Calendar lastMonth = Calendar.getInstance();
lastMonth.setTime(DateTool.getNextMonth(currentDate, -1));
day += lastMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
}

// System.out.println("bornDate="+DateTool.getFullDate().format(bornDate));
// System.out.println("currentDate="+DateTool.getFullDate().format(currentDate));
// System.out.println("oneYear="+DateTool.getFullDate().format(oneYear));
// System.out.println("year="+year);
// System.out.println("month="+month);
// System.out.println("day="+day);
if(year < 1){ //小于1岁
if(month > 0){
age += month + "个月";
}
if(day >= 0){
if(StringUtils.isNotBlank(age)){
age += "零" + day + "天";
}else{
age += day + "天";
}
}
}else{
age += year + "岁";
if(month > 0){
age += month + "个月";
}else{
if(day > 0){
age += "零" + day + "天";
}
}
}
return age;
}
第4个回答  2009-05-12
我的构思是:

(now - bir)/1000/60/60/24

now是现在的time的long表示
bir是出生的time的long表示
相似回答