Calculate Age

1 vote · 1 comment

self explanatory

raw ·
copy
· download
public int getAge (int _year, int _month, int _day) { GregorianCalendar cal = new GregorianCalendar(); int y, m, d, a; y = cal.get(Calendar.YEAR); m = cal.get(Calendar.MONTH); d = cal.get(Calendar.DAY_OF_MONTH); cal.set(_year, _month, _day); a = y - cal.get(Calendar.YEAR); if ((m < cal.get(Calendar.MONTH)) || ((m == cal.get(Calendar.MONTH)) && (d < cal .get(Calendar.DAY_OF_MONTH)))) { --a; } if(a < 0) throw new IllegalArgumentException("Age < 0"); return a; }
Add a comment

1 Comment

Small modification: Change m = cal.get(Calendar.MONTH); to m = cal.get(Calendar.MONTH) + 1;

This is why

Reply · Sept. 12, 2011, 1:36 p.m.