Get Age from a Birth Date

Get the age of a person from their birth date.

Javadoc available at https://www.javatapas.com/docs/javatapas/util/GetAgeFromBirthDate.html


public static int getAgeFromBirthDate(Calendar birthDate, Calendar refDate){

	int refDay = refDate.get(Calendar.DAY_OF_MONTH);
	int refMonth = refDate.get(Calendar.MONTH) + 1;
	int refYear = refDate.get(Calendar.YEAR);

  int birthDay = birthDate.get(Calendar.DAY_OF_MONTH);
	int birthMonth = birthDate.get(Calendar.MONTH) + 1;
	int birthYear = birthDate.get(Calendar.YEAR);

	int age = refYear - birthYear;
	if ((refMonth < birthMonth) || (refMonth == birthMonth && refDay < birthDay)){--age;}

	return age;
}

public static int getAgeFromBirthDate(Calendar birthDate){
  return getAgeFromBirthDate(birthDate, Calendar.getInstance());
}

public static int getAgeFromBirthDate(int birthDay, int birthMonth, int birthYear){
  Calendar calendar = Calendar.getInstance();
  calendar.set(birthYear, birthMonth - 1, birthDay, 0, 0);
  return getAgeFromBirthDate(calendar);
}