I am making an application, where when entering the date of birth I calculate the age, but the key is that if the next birthday is closer to the previous one already celebrated, I will increase one year at the age. I have this code but I can not get it to work well, especially if I meet at the beginning of the year and we are at the end, I do not add a year.
private String getAge(int year, int month, int day) {
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dob.set(year, month, day);
int diaAsegurado = dob.get(Calendar.DAY_OF_YEAR);
int diaHoy = today.get(Calendar.DAY_OF_YEAR);
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (diaHoy < diaAsegurado) {
if ((diaAsegurado - diaHoy) > (today.getActualMaximum(Calendar.DAY_OF_YEAR) / 3)) {
age--;
}
}
Integer ageInt = new Integer(age);
String ageS = ageInt.toString();
return ageS;
}