Java tutorial
//package com.java2s; import java.util.Calendar; import java.util.Date; public class Main { public static int getUserAge(Date birthDay) { Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) { return 0; } int yearNow = cal.get(Calendar.YEAR); int monthNow = cal.get(Calendar.MONTH); int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); if (null == birthDay) { return 0; } else { cal.setTime(birthDay); int yearBirth = cal.get(Calendar.YEAR); int monthBirth = cal.get(Calendar.MONTH); int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); int age = yearNow - yearBirth; if (monthNow <= monthBirth) { if (monthNow == monthBirth) { // monthNow==monthBirth if (dayOfMonthNow < dayOfMonthBirth) { age--; } else { // do nothing } } else { // monthNow>monthBirth age--; } } else { // monthNow<monthBirth // donothing } return age; } } }