Java Age Calculate age(int year, int month, int date)

Here you can find the source of age(int year, int month, int date)

Description

This method returns the age on the basis of parameter passed to the method.

License

Open Source License

Parameter

Parameter Description
year a parameter
month a parameter
date a parameter

Return

age

Declaration

public static int age(int year, int month, int date) 

Method Source Code


//package com.java2s;
import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    /**//  w  w w . j av  a 2  s .  com
     * This method returns the age on the basis of parameter passed to the method.
     *
     * @param year
     * @param month
     * @param date
     * @return age
     */
    public static int age(int year, int month, int date) {
        Calendar cal = new GregorianCalendar(year, month, date);
        Calendar now = new GregorianCalendar();
        int age = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
        if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
                || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
                        && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) {
            age--;
        }
        return age;
    }
}

Related

  1. age(Date birthdate, Date asOf)
  2. age(Date dob)
  3. ageAcceptable(final Date doB, final Date now, final int acceptedAge)
  4. ageInYears(java.util.Date dateUtil)
  5. calculateAge(Date aBirthdate)
  6. calculateAge(Date aDateFrom, Date aDateTo)