How do I calculate someone's age in Java?

前端 未结 28 2350
渐次进展
渐次进展 2020-11-22 02:20

I want to return an age in years as an int in a Java method. What I have now is the following where getBirthDate() returns a Date object (with the birth date ;-)):



        
28条回答
  •  长发绾君心
    2020-11-22 02:58

    /**
     * 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) {
    
        Calendar today = Calendar.getInstance();
        Calendar birthDate = Calendar.getInstance();
    
        int age = 0;
    
        birthDate.setTime(dateOfBirth);
        if (birthDate.after(today)) {
            throw new IllegalArgumentException("Can't be born in the future");
        }
    
        age = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
    
        // If birth date is greater than todays date (after 2 days adjustment of leap year) then decrement age one year   
        if ( (birthDate.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR) > 3) ||
                (birthDate.get(Calendar.MONTH) > today.get(Calendar.MONTH ))){
            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 ((birthDate.get(Calendar.MONTH) == today.get(Calendar.MONTH )) &&
                  (birthDate.get(Calendar.DAY_OF_MONTH) > today.get(Calendar.DAY_OF_MONTH ))){
            age--;
        }
    
        return age;
    }
    

提交回复
热议问题