I want to return an age in years as an int in a Java method. What I have now is the following where getBirthDate() returns a Date object (with the birth date ;-)):
What about this one?
public Integer calculateAge(Date date) { if (date == null) { return null; } Calendar cal1 = Calendar.getInstance(); cal1.setTime(date); Calendar cal2 = Calendar.getInstance(); int i = 0; while (cal1.before(cal2)) { cal1.add(Calendar.YEAR, 1); i += 1; } return i; }