Java Age Calculate getAgeFromDate(Date snapshotDate, Date birthdate)

Here you can find the source of getAgeFromDate(Date snapshotDate, Date birthdate)

Description

get Age From Date

License

Apache License

Declaration

public static int getAgeFromDate(Date snapshotDate, Date birthdate) 

Method Source Code

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

Related

  1. getAge1(Date birthDay)
  2. getAgeAt(final Date birthday, final Date date)
  3. getAgeFromBirthDate(Date birth, boolean estimate)
  4. getAgeFromBirthday(Date birthday)
  5. getAgeFromBirthday(Date date)