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: 594962 $ $Date: 2007-11-14 18:30:27 +0100 (Mi, 14 Nov 2007) $
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 */
036public class DateValidator {
037
038    /**
039     * Singleton instance of this class.
040     */
041    private static final DateValidator DATE_VALIDATOR = new DateValidator();
042
043    /**
044     * Returns the Singleton instance of this validator.
045     * @return A singleton instance of the DateValidator.
046     */
047    public static DateValidator getInstance() {
048        return DATE_VALIDATOR;
049    }
050
051    /**
052     * Protected constructor for subclasses to use.
053     */
054    protected DateValidator() {
055        super();
056    }
057
058    /**
059     * <p>Checks if the field is a valid date.  The pattern is used with
060     * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
061     * length will be checked so '2/12/1999' will not pass validation with
062     * the format 'MM/dd/yyyy' because the month isn't two digits.
063     * The setLenient method is set to <code>false</code> for all.</p>
064     *
065     * @param value The value validation is being performed on.
066     * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
067     * @param strict Whether or not to have an exact match of the datePattern.
068     * @return true if the date is valid.
069     */
070    public boolean isValid(String value, String datePattern, boolean strict) {
071
072        if (value == null
073                || datePattern == null
074                || datePattern.length() <= 0) {
075
076            return false;
077        }
078
079        SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
080        formatter.setLenient(false);
081
082        try {
083            formatter.parse(value);
084        } catch(ParseException e) {
085            return false;
086        }
087
088        if (strict && (datePattern.length() != value.length())) {
089            return false;
090        }
091
092        return true;
093    }
094
095    /**
096     * <p>Checks if the field is a valid date.  The <code>Locale</code> is
097     * used with <code>java.text.DateFormat</code>.  The setLenient method
098     * is set to <code>false</code> for all.</p>
099     *
100     * @param value The value validation is being performed on.
101     * @param locale The locale to use for the date format, defaults to the default
102     * system default if null.
103     * @return true if the date is valid.
104     */
105    public boolean isValid(String value, Locale locale) {
106
107        if (value == null) {
108            return false;
109        }
110
111        DateFormat formatter = null;
112        if (locale != null) {
113            formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
114        } else {
115            formatter =
116                    DateFormat.getDateInstance(
117                            DateFormat.SHORT,
118                            Locale.getDefault());
119        }
120
121        formatter.setLenient(false);
122
123        try {
124            formatter.parse(value);
125        } catch(ParseException e) {
126            return false;
127        }
128
129        return true;
130    }
131
132}