Java Age Calculate calculateAge(Date DOB)

Here you can find the source of calculateAge(Date DOB)

Description

Calculates age w.r.t provided DOB

License

Open Source License

Parameter

Parameter Description
date SQL formatted date string.

Return

age

Declaration

public static synchronized int calculateAge(Date DOB) 

Method Source Code

//package com.java2s;
/*//  w  w  w.  j a v a  2 s  .c o m
 * $Id$
 *
 * Authors:
 *      Jeff Buchbinder <jeff@freemedsoftware.org>
 *
 * FreeMED Electronic Medical Record and Practice Management System
 * Copyright (C) 1999-2012 FreeMED Software Foundation
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

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

public class Main {
    /**
     * Calculates age w.r.t provided DOB
     * 
     * @param date
     *            SQL formatted date string.
     * @return age
     */
    public static synchronized int calculateAge(Date DOB) {
        int age = -1;
        Calendar calDOB = new GregorianCalendar();
        calDOB.setTime(DOB);
        Calendar calNow = new GregorianCalendar();
        calNow.setTime(new Date());
        age = calNow.get(Calendar.YEAR) - calDOB.get(Calendar.YEAR);
        return age;
    }
}

Related

  1. calculateAge(Date aBirthdate)
  2. calculateAge(Date aDateFrom, Date aDateTo)
  3. calculateAge(Date birthDate)
  4. calculateAge(Date birthday)
  5. calculateAge(Date dateOfBirth)
  6. calculateAge(Date dob)
  7. calculateAgeInMonth(final Date birthDate, final Date asOfDate)
  8. calculateAgeInWeek(final Date birthDate, final Date asOfDate)
  9. getAge(Date birthDate, Date controlDate)