Here you can find the source of getAge1(Date birthDay)
public static String getAge1(Date birthDay) throws Exception
//package com.java2s; //License from project: Apache License import java.util.Calendar; import java.util.Date; public class Main { public static String getAge1(Date birthDay) throws Exception { if (null == birthDay) return "-"; Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) { throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!"); }// w ww . j a v a 2 s . com int yearNow = cal.get(Calendar.YEAR); int monthNow = cal.get(Calendar.MONTH); int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); 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 // donothing } if (age == 0) return "0"; return String.valueOf(age); } }