How do I calculate someone's age in Java?

前端 未结 28 2354
渐次进展
渐次进展 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;
    }
    
    0 讨论(0)
  • 2020-11-22 02:58

    With Java 8, we can calculate a person age with one line of code:

    public int calCAge(int year, int month,int days){             
        return LocalDate.now().minus(Period.of(year, month, days)).getYear();         
    }
    
    0 讨论(0)
  • 2020-11-22 03:01

    The fields birth and effect are both date fields:

    Calendar bir = Calendar.getInstance();
    bir.setTime(birth);
    int birthNm = bir.get(Calendar.DAY_OF_YEAR);
    int birthYear = bir.get(Calendar.YEAR);
    Calendar eff = Calendar.getInstance();
    eff.setTime(effect);
    

    This basically a modification of John O's solution without using depreciated methods. I spent a fair amount of time trying to get his code to work in in my code. Maybe this will save others that time.

    0 讨论(0)
  • 2020-11-22 03:01
    public int getAge(Date dateOfBirth) 
    {
        Calendar now = Calendar.getInstance();
        Calendar dob = Calendar.getInstance();
    
        dob.setTime(dateOfBirth);
    
        if (dob.after(now)) 
        {
            throw new IllegalArgumentException("Can't be born in the future");
        }
    
        int age = now.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
    
        if (now.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) 
        {
            age--;
        }
    
        return age;
    }
    
    0 讨论(0)
  • 2020-11-22 03:01
    public int getAge(Date birthDate) {
        Calendar a = Calendar.getInstance(Locale.US);
        a.setTime(date);
        Calendar b = Calendar.getInstance(Locale.US);
        int age = b.get(YEAR) - a.get(YEAR);
        if (a.get(MONTH) > b.get(MONTH) || (a.get(MONTH) == b.get(MONTH) && a.get(DATE) > b.get(DATE))) {
            age--;
        }
        return age;
    }
    
    0 讨论(0)
  • 2020-11-22 03:02

    It's perhaps surprising to note that you don't need to know how many days or months there are in a year or how many days are in those months, likewise, you don't need to know about leap years, leap seconds, or any of that stuff using this simple, 100% accurate method:

    public static int age(Date birthday, Date date) {
        DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        int d1 = Integer.parseInt(formatter.format(birthday));
        int d2 = Integer.parseInt(formatter.format(date));
        int age = (d2-d1)/10000;
        return age;
    }
    
    0 讨论(0)
提交回复
热议问题