Java Utililty Methods Age Calculate

List of utility methods to do Age Calculate

Description

The list of methods to do Age Calculate are organized into topic(s).

Method

IntegercalculateAge(Date dob)
calculate Age
Calendar cal = new GregorianCalendar();
cal.setTime(dob);
Calendar now = new GregorianCalendar();
int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
        || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
                && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) {
    res--;
...
intcalculateAge(Date DOB)
Calculates age w.r.t provided DOB
int age = -1;
Calendar calDOB = new GregorianCalendar();
calDOB.setTime(DOB);
Calendar calNow = new GregorianCalendar();
calNow.setTime(new Date());
age = calNow.get(Calendar.YEAR) - calDOB.get(Calendar.YEAR);
return age;
intcalculateAgeInMonth(final Date birthDate, final Date asOfDate)
Calculate age based on the birth date and the reference date.
Calendar birthCalendar = Calendar.getInstance();
birthCalendar.setTime(birthDate);
Calendar todayCalendar = Calendar.getInstance();
todayCalendar.setTime(asOfDate);
int birthYear = birthCalendar.get(Calendar.YEAR);
int todayYear = todayCalendar.get(Calendar.YEAR);
int ageInYear = todayYear - birthYear;
int birthMonth = birthCalendar.get(Calendar.MONTH);
...
intcalculateAgeInWeek(final Date birthDate, final Date asOfDate)
Calculate age based on the birth date and the reference date.
Calendar birthCalendar = Calendar.getInstance();
birthCalendar.setTime(birthDate);
Calendar todayCalendar = Calendar.getInstance();
todayCalendar.setTime(asOfDate);
long timeDifference = (todayCalendar.getTimeInMillis() - birthCalendar.getTimeInMillis()) / 1000;
int ageInWeek = (int) (timeDifference / ONE_WEEK);
if (timeDifference % ONE_WEEK > ONE_WEEK / 2)
    ageInWeek++;
...
IntegergetAge(Date birthDate, Date controlDate)
age = the_year_of_controlDate - the_year_of_birthDate
Integer age = -1;
if (birthDate.before(controlDate)) {
    Calendar dateOfBirth = Calendar.getInstance();
    dateOfBirth.setTime(birthDate);
    Calendar theDate = Calendar.getInstance();
    theDate.setTime(controlDate);
    age = theDate.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);
    Calendar today = Calendar.getInstance();
...
intgetAge(Date birthDay)
get Age
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);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
...
floatgetAge(Date birthday)
get Age
Calendar calendar = Calendar.getInstance();
int yearNow = calendar.get(Calendar.YEAR);
int monthNow = calendar.get(Calendar.MONTH);
calendar.setTime(birthday);
int yearBorn = calendar.get(Calendar.YEAR);
int monthBorn = calendar.get(Calendar.MONTH);
int year = yearNow - yearBorn;
if (year >= 1)
...
intgetAge(Date birthday)
get Age
Calendar birthCal = Calendar.getInstance();
birthCal.setTime(birthday);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - birthCal.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) < birthCal.get(Calendar.DAY_OF_YEAR)) {
    age--;
return age;
...
IntegergetAge(Date birthday)
get Age
if (birthday == null) {
    return null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(birthday);
int startY = calendar.get(Calendar.YEAR);
calendar.setTime(new Date());
int endY = calendar.get(Calendar.YEAR);
...
intgetAge(Date date)
get Age
Calendar now = Calendar.getInstance();
Calendar dob = Calendar.getInstance();
dob.setTime(date);
if (dob.after(now)) {
    throw new IllegalArgumentException("Can't be born in the future");
int year1 = now.get(Calendar.YEAR);
int year2 = dob.get(Calendar.YEAR);
...