public static int getAge(Date birthday) {
if (birthday == null) {
return 0;
}
Calendar born = Calendar.getInstance();
Calendar now = Calendar.getInstance();
now.setTime(new Date());
born.setTime(birthday);
int year1 = now.get(Calendar.YEAR);
int year2 = born.get(Calendar.YEAR);
int age = year1 - year2;
int month1 = now.get(Calendar.MONTH);
int month2 = born.get(Calendar.MONTH);
if (month2 > month1) {
age--;
} else if (month1 == month2) {
int day1 = now.get(Calendar.DAY_OF_MONTH);
int day2 = born.get(Calendar.DAY_OF_MONTH);
if (day2 > day1) {
age--;
}
}
return age;
}