Java Age Calculate ageAcceptable(final Date doB, final Date now, final int acceptedAge)

Here you can find the source of ageAcceptable(final Date doB, final Date now, final int acceptedAge)

Description

age Acceptable

License

Open Source License

Parameter

Parameter Description
dob Date of Birth
now substitute for now, can be null which will default to now()
acceptedAge Minimum age which must be passed

Declaration

public static boolean ageAcceptable(final Date doB, final Date now,
        final int acceptedAge) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * CDDL HEADER START//from www  .  j a  v a 2s . co m
 * 
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 * 
 * You can obtain a copy of the license at
 * src/com/vodafone360/people/VODAFONE.LICENSE.txt or
 * http://github.com/360/360-Engine-for-Android
 * See the License for the specific language governing permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL HEADER in each file and
 *  include the License file at src/com/vodafone360/people/VODAFONE.LICENSE.txt.
 * If applicable, add the following below this CDDL HEADER, with the fields
 * enclosed by brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 * 
 * CDDL HEADER END
 * 
 * Copyright 2010 Vodafone Sales & Services Ltd.  All rights reserved.
 * Use is subject to license terms.
 ******************************************************************************/

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

public class Main {
    /**
     * @param dob Date of Birth
     * @param now substitute for now, can be null which will default to now()
     * @param acceptedAge Minimum age which must be passed 
     * @return
     */
    public static boolean ageAcceptable(final Date doB, final Date now,
            final int acceptedAge) {
        Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.setTime(doB);

        Calendar today = Calendar.getInstance();

        if (now != null)
            today.setTime(now);

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

        if (dateOfBirth.get(Calendar.MONTH) > today.get(Calendar.MONTH)) {
            age--;
        } else if (dateOfBirth.get(Calendar.MONTH) == today
                .get(Calendar.MONTH)
                && dateOfBirth.get(Calendar.DAY_OF_MONTH) > today
                        .get(Calendar.DAY_OF_MONTH)) {
            age--;
        }

        return age >= acceptedAge;
    }
}

Related

  1. age(Date birthdate, Date asOf)
  2. age(Date dob)
  3. age(int year, int month, int date)
  4. ageInYears(java.util.Date dateUtil)
  5. calculateAge(Date aBirthdate)
  6. calculateAge(Date aDateFrom, Date aDateTo)
  7. calculateAge(Date birthDate)