Here you can find the source of getAgeFromDate(Date snapshotDate, Date birthdate)
public static int getAgeFromDate(Date snapshotDate, Date birthdate)
//package com.java2s; //License from project: Apache License import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { public static int getAgeFromDate(Date snapshotDate, Date birthdate) { try {//from ww w . jav a 2 s . c o m Calendar dateOfBirth = new GregorianCalendar(); dateOfBirth.setTime(birthdate); Calendar asOfDate = Calendar.getInstance(); if (snapshotDate != null) asOfDate.setTime(snapshotDate); int age = asOfDate.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR); dateOfBirth.add(Calendar.YEAR, age); if (asOfDate.before(dateOfBirth)) age--; return age; } catch (Exception e) { e.printStackTrace(); } return -1; } }