Calculate age from given date in Android

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; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.