Java Age Calculate getAge(Date birthDay)

Here you can find the source of getAge(Date birthDay)

Description

get Age

License

Apache License

Declaration

public static int getAge(Date birthDay) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;

public class Main {

    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birthDay);//from  w ww  . ja v a  2 s  .  c  om
        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;
    }
}

Related

  1. calculateAgeInWeek(final Date birthDate, final Date asOfDate)
  2. getAge(Date birthDate, Date controlDate)
  3. getAge(Date birthday)
  4. getAge(Date birthday)
  5. getAge(Date birthday)
  6. getAge(Date date)
  7. getAge(Date date, TimeZone tz)
  8. getAge(Date dateNaissance)
  9. getAge(Date time, Date birth)