Java Age Calculate getAgeAt(final Date birthday, final Date date)

Here you can find the source of getAgeAt(final Date birthday, final Date date)

Description

get Age At

License

Open Source License

Declaration

public static int getAgeAt(final Date birthday, final Date date) 

Method Source Code

//package com.java2s;

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

public class Main {
    public static int getAgeAt(final Date birthday, final Date date) {

        if (birthday == null || date == null || date.compareTo(birthday) < 0) {
            return 0;
        }//from   w ww.j a v  a2 s . c  om

        final Calendar cal = Calendar.getInstance();
        cal.setTime(birthday);
        final int birthYear = cal.get(Calendar.YEAR);
        final int birthMonth = cal.get(Calendar.MONTH);
        final int birthDay = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(date);
        final int year = cal.get(Calendar.YEAR);
        final int month = cal.get(Calendar.MONTH);
        final int day = cal.get(Calendar.DAY_OF_MONTH);

        int age = year - birthYear;
        if (month < birthMonth || (month == birthMonth && day < birthDay)) {
            age--;
        }

        return age;
    }
}

Related

  1. getAge(Date dateNaissance)
  2. getAge(Date time, Date birth)
  3. getAge(final Date birthDay, final Date currDate)
  4. getAge(String year)
  5. getAge1(Date birthDay)
  6. getAgeFromBirthDate(Date birth, boolean estimate)
  7. getAgeFromBirthday(Date birthday)
  8. getAgeFromBirthday(Date date)
  9. getAgeFromDate(Date snapshotDate, Date birthdate)