public int calculaIdade(String data) throws ParseException{
int idade = 0;
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date data1 = df.parse(data);
Date data2 = new Date();
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(data1);
cal2.setTime(data2);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
int month1 = cal1.get(Calendar.MONTH);
int month2 = cal2.get(Calendar.MONTH);
int day1 = cal1.get(Calendar.DAY_OF_MONTH);
int day2 = cal2.get(Calendar.DAY_OF_MONTH);
idade = year2 - year1;
//mês menor que o mês passado como parêmtro ou
//mês igual e dia menor que o passado como parâmetro
//idade - 1.
if ( (month2 < month1)
|| ( (month2 == month1) && (day2 < day1) ) ) {
idade -= 1;
}
return idade;
}
//Simples assim:
private static final int calculateAge(final Date dtNascimento)
{
int idade;
final Calendar calAtual = Calendar.getInstance();
final Calendar calNascimento = new GregorianCalendar();
calNascimento.setTime(dtNascimento);
idade = calAtual.get(Calendar.YEAR) - calNascimento.get(Calendar.YEAR);
if (calAtual.get(Calendar.DAY_OF_YEAR) > calNascimento.get(Calendar.DAY_OF_YEAR))
return idade;
return --idade;
}