Java Age Calculate getAgeFromBirthday(Date birthday)

Here you can find the source of getAgeFromBirthday(Date birthday)

Description

get Age From Birthday

License

Open Source License

Declaration

public static Integer getAgeFromBirthday(Date birthday) 

Method Source Code

//package com.java2s;
/**/*  w  ww . j  a v a2  s  .  com*/
 *  Copyright 2010 Society for Health Information Systems Programmes, India (HISP India)
 *
 *  This file is part of IPD module.
 *
 *  IPD module is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
    
 *  IPD module is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with IPD module.  If not, see <http://www.gnu.org/licenses/>.
 *
 **/

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static Integer getAgeFromBirthday(Date birthday) {
        if (birthday == null) {
            return null;
        }
        // Create a calendar object with the date of birth
        Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.setTime(birthday);
        // Create a calendar object with today's date
        Calendar today = Calendar.getInstance();

        // Get age based on year
        int age = today.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);

        // Add the tentative age to the date of birth to get this year's birthday
        dateOfBirth.add(Calendar.YEAR, age);

        // If this year's birthday has not happened yet, subtract one from age
        if (today.before(dateOfBirth)) {
            age--;
        }

        return age;
    }
}

Related

  1. getAge(final Date birthDay, final Date currDate)
  2. getAge(String year)
  3. getAge1(Date birthDay)
  4. getAgeAt(final Date birthday, final Date date)
  5. getAgeFromBirthDate(Date birth, boolean estimate)
  6. getAgeFromBirthday(Date date)
  7. getAgeFromDate(Date snapshotDate, Date birthdate)