    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.validator;
    018
    019import java.text.DateFormat;
    020import java.text.ParseException;
    021import java.text.SimpleDateFormat;
    022import java.util.Locale;
    023
    024/**
    025 * <p>Perform date validations.</p>
    026 * <p>
    027 * This class is a Singleton; you can retrieve the instance via the
    028 * getInstance() method.
    029 * </p>
    030 *
    031 * @version $Revision$
    032 * @since Validator 1.1
    033 * @deprecated Use the new DateValidator, CalendarValidator or TimeValidator in the
    034 * routines package. This class will be removed in a future release.
    035 */
    036@Deprecated
    037public class DateValidator {
    038
    039    /**
    040     * Singleton instance of this class.
    041     */
    042    private static final DateValidator DATE_VALIDATOR = new DateValidator();
    043
    044    /**
    045     * Returns the Singleton instance of this validator.
    046     * @return A singleton instance of the DateValidator.
    047     */
    048    public static DateValidator getInstance() {
    049        return DATE_VALIDATOR;
    050    }
    051
    052    /**
    053     * Protected constructor for subclasses to use.
    054     */
    055    protected DateValidator() {
    056        super();
    057    }
    058
    059    /**
    060     * <p>Checks if the field is a valid date.  The pattern is used with
    061     * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
    062     * length will be checked so '2/12/1999' will not pass validation with
    063     * the format 'MM/dd/yyyy' because the month isn't two digits.
    064     * The setLenient method is set to <code>false</code> for all.</p>
    065     *
    066     * @param value The value validation is being performed on.
    067     * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
    068     * @param strict Whether or not to have an exact match of the datePattern.
    069     * @return true if the date is valid.
    070     */
    071    public boolean isValid(String value, String datePattern, boolean strict) {
    072
    073        if (value == null
    074                || datePattern == null
    075                || datePattern.length() <= 0) {
    076
    077            return false;
    078        }
    079
    080        SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
    081        formatter.setLenient(false);
    082
    083        try {
    084            formatter.parse(value);
    085        } catch(ParseException e) {
    086            return false;
    087        }
    088
    089        if (strict && (datePattern.length() != value.length())) {
    090            return false;
    091        }
    092
    093        return true;
    094    }
    095
    096    /**
    097     * <p>Checks if the field is a valid date.  The <code>Locale</code> is
    098     * used with <code>java.text.DateFormat</code>.  The setLenient method
    099     * is set to <code>false</code> for all.</p>
    100     *
    101     * @param value The value validation is being performed on.
    102     * @param locale The locale to use for the date format, defaults to the default
    103     * system default if null.
    104     * @return true if the date is valid.
    105     */
    106    public boolean isValid(String value, Locale locale) {
    107
    108        if (value == null) {
    109            return false;
    110        }
    111
    112        DateFormat formatter = null;
    113        if (locale != null) {
    114            formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    115        } else {
    116            formatter =
    117                    DateFormat.getDateInstance(
    118                            DateFormat.SHORT,
    119                            Locale.getDefault());
    120        }
    121
    122        formatter.setLenient(false);
    123
    124        try {
    125            formatter.parse(value);
    126        } catch(ParseException e) {
    127            return false;
    128        }
    129
    130        return true;
    131    }
    132
    133}