Perform date validations : Date « Data Type « Java Tutorial






/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * <p>Perform date validations.</p>
 * <p>
 * This class is a Singleton; you can retrieve the instance via the
 * getInstance() method.
 * </p>
 *
 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
 * @since Validator 1.1
 */
public class DateValidator {

    /**
     * Singleton instance of this class.
     */
    private static final DateValidator DATE_VALIDATOR = new DateValidator();

    /**
     * Returns the Singleton instance of this validator.
     * @return A singleton instance of the DateValidator.
     */
    public static DateValidator getInstance() {
        return DATE_VALIDATOR;
    }

    /**
     * Protected constructor for subclasses to use.
     */
    protected DateValidator() {
        super();
    }

    /**
     * <p>Checks if the field is a valid date.  The pattern is used with
     * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
     * length will be checked so '2/12/1999' will not pass validation with
     * the format 'MM/dd/yyyy' because the month isn't two digits.
     * The setLenient method is set to <code>false</code> for all.</p>
     *
     * @param value The value validation is being performed on.
     * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
     * @param strict Whether or not to have an exact match of the datePattern.
     * @return true if the date is valid.
     */
    public boolean isValid(String value, String datePattern, boolean strict) {

        if (value == null
                || datePattern == null
                || datePattern.length() <= 0) {

            return false;
        }

        SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
        formatter.setLenient(false);

        try {
            formatter.parse(value);
        } catch(ParseException e) {
            return false;
        }

        if (strict && (datePattern.length() != value.length())) {
            return false;
        }

        return true;
    }

    /**
     * <p>Checks if the field is a valid date.  The <code>Locale</code> is
     * used with <code>java.text.DateFormat</code>.  The setLenient method
     * is set to <code>false</code> for all.</p>
     *
     * @param value The value validation is being performed on.
     * @param locale The locale to use for the date format, defaults to the default
     * system default if null.
     * @return true if the date is valid.
     */
    public boolean isValid(String value, Locale locale) {

        if (value == null) {
            return false;
        }

        DateFormat formatter = null;
        if (locale != null) {
            formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        } else {
            formatter =
                    DateFormat.getDateInstance(
                            DateFormat.SHORT,
                            Locale.getDefault());
        }

        formatter.setLenient(false);

        try {
            formatter.parse(value);
        } catch(ParseException e) {
            return false;
        }

        return true;
    }

}








2.38.Date
2.38.1.The java.util.Date Class
2.38.2.Show date and time using only Date methods.
2.38.3.Obtaining a Date Object From a String
2.38.4.Convert from a java.util.Date Object to a java.sql.Date Object
2.38.5.Convert a String Date Such as 2003/01/10 into a java.util.Date Object
2.38.6.Create Yesterday's Date from a Date in the String Format of MM/DD/YYYY
2.38.7.Create a java.util.Date Object from a Year, Month, Day Format
2.38.8.Create a java.sql.Time Object from java.util.Date
2.38.9.Convert the Current Time to a java.sql.Date Object
2.38.10.Convert Date into milliseconds example
2.38.11.Create java Date from specific time example
2.38.12.Determine the Day of the Week from Today's Date
2.38.13.ISO 8601 date parsing utility.
2.38.14.Parses a string representing a date by trying a variety of different parsers.
2.38.15.Perform date validations