Validates the birth date by checking if a) the preferred date is before the current date or b) the preferred date is before 1916 and correcting it if needed. - Android java.lang

Android examples for java.lang:Math Geometry

Description

Validates the birth date by checking if a) the preferred date is before the current date or b) the preferred date is before 1916 and correcting it if needed.

Demo Code

/*/*from   w  ww.  ja va2s.  co  m*/
 * CDDL HEADER START
 *
 * 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.
 */
//package com.java2s;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    /**
     * The smallest birth date that can be chosen in Android as defined in the DatePicker.java is
     * 1900. The DateUtils in Android apparently have a bug that mixes up dates before 1916. Please
     * also take a look at the MathUtilsTest.java for this bug.
     * (check Android source code: http://tinyurl.com/39ppcfc).
     */
    private static final int MINIMUM_BIRTH_DATE = 1916;
    /**   The month January as a number representation. */
    private static final int JANUARY = 1;
    /** The first day of the month. */
    private static final int FIRST_DAY_OF_MONTH = 1;

    /**
     * 
     * Validates the birth date by checking if 
     * <p>
     * a) the preferred date is before the current date or
     * b) the preferred date is before 1916
     * and correcting it if needed. E.g. the 32nd of January will be turned into the 1st of
     * February.
     * 
     * 
     * @param year The preferred year of the birth date.
     * @param monthOfYear The preferred month of the year.
     * @param dayOfMonth The preferred day of the month.
     * 
     * @return A Gregorian calendar object containing the corrected date or the date that was passed
     * if it was already in a correct format.
     * 
     */
    public static GregorianCalendar getValidatedBirthdate(final int year,
            final int monthOfYear, final int dayOfMonth) {
        GregorianCalendar currentDate = new GregorianCalendar();
        GregorianCalendar preferredDate = new GregorianCalendar(year,
                monthOfYear, dayOfMonth);
        GregorianCalendar oldestDate = new GregorianCalendar(
                MINIMUM_BIRTH_DATE, JANUARY, FIRST_DAY_OF_MONTH);

        if (preferredDate.before(oldestDate)) { // is the birthdate before 1916 (android.DateUtils) 
            // or 1900 (android.widget.DatePicker)? platform limitation!
            // return the oldest possible year (1916) with the month and the date entered by the user
            oldestDate.set(Calendar.YEAR, MINIMUM_BIRTH_DATE);
            oldestDate.set(Calendar.MONTH,
                    preferredDate.get(Calendar.MONTH));
            oldestDate.set(Calendar.DAY_OF_MONTH,
                    preferredDate.get(Calendar.DAY_OF_MONTH));

            return oldestDate;
        } else if (currentDate.before(preferredDate)) { // is the birthdate entered in the future?
            // enter the current date
            return currentDate;
        }

        return preferredDate;
    }
}

Related Tutorials