Java Calendar Calculate calcAge(Calendar dateOfBirth, Calendar now)

Here you can find the source of calcAge(Calendar dateOfBirth, Calendar now)

Description

Calculates the age if date of birth is given (for a calendar time stamp)

License

EUPL

Parameter

Parameter Description
dateOfBirth Date of Birth
now Calendar time stamp at which the age needs to be calculated for

Return

Age of a person

Declaration

public static int calcAge(Calendar dateOfBirth, Calendar now) 

Method Source Code

//package com.java2s;
/*//w  w  w  . ja v a2  s  .com
 * Copyright 2003 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 */

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

public class Main {
    /**
     * Calculates the age if date of birth is given (for a calendar time stamp)
     * @param dateOfBirth Date of Birth
     * @param now Calendar time stamp at which the age needs to be calculated for
     * @return Age of a person
     */
    public static int calcAge(Calendar dateOfBirth, Calendar now) {
        int age = now.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);

        int nowM = now.get(Calendar.MONTH);
        int dobM = dateOfBirth.get(Calendar.MONTH);
        int nowDOM = now.get(Calendar.DAY_OF_MONTH);
        int dobDOM = dateOfBirth.get(Calendar.DAY_OF_MONTH);

        if ((nowM < dobM) || ((nowM == dobM) && (nowDOM < dobDOM))) {
            age--;
        }

        if (age < 0) {
            throw new IllegalArgumentException("Calculated age results in negative value.");
        }
        return age;
    }

    /**
     * Calculates the age if date of birth is given as Calendar object
     * @param dateOfBirth Date of Birth as Calendar object
     * @return Age of a person
     */
    public static int calcAge(Calendar dateOfBirth) {
        return calcAge(dateOfBirth, Calendar.getInstance());
    }

    /**
     * Calculates the age if date of birth is given (for a date time stamp)
     * @param dateOfBirth Date of Birth
     * @param now Date time stamp at which the age needs to be calculated for
     * @return Age of a person
     */
    public static int calcAge(Date dateOfBirth, Date now) {
        Calendar dob = Calendar.getInstance();
        dob.setTime(dateOfBirth);
        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(now);
        return calcAge(dob, nowCal);
    }

    /**
     * Calculates the age if date of birth is given as Date object
     * @param dateOfBirth Date of Birth as Date object
     * @return Age of a person
     */
    public static int calcAge(Date dateOfBirth) {
        return calcAge(dateOfBirth, new Date());
    }
}

Related

  1. addQuarter(Calendar calendar, int quarterDelta)
  2. addTime(int calendarType, long time, int number)
  3. alignHours(int interval, Calendar timestamp)
  4. alignSecond(Calendar timestamp)
  5. cal2Str(Calendar pCal)
  6. calcDifferenceAsDays(Calendar aBaseDate, Calendar aTargetDate)
  7. calcDifferenceAsYearsPrivate(Calendar aBaseDate, Calendar aTargetDate)
  8. calcTime(Date end, Date start, int calendarField)
  9. calcTimezone(Calendar cal)