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 * @since 1.1
032 * @deprecated Use the new DateValidator, CalendarValidator or TimeValidator in the
033 * routines package. This class will be removed in a future release.
034 */
035@Deprecated
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    }
056
057    /**
058     * <p>Checks if the field is a valid date.  The <code>Locale</code> is
059     * used with <code>java.text.DateFormat</code>.  The setLenient method
060     * is set to {@code false} for all.</p>
061     *
062     * @param value The value validation is being performed on.
063     * @param locale The locale to use for the date format, defaults to the default
064     * system default if null.
065     * @return true if the date is valid.
066     */
067    public boolean isValid(final String value, final Locale locale) {
068
069        if (value == null) {
070            return false;
071        }
072
073        DateFormat formatter;
074        if (locale != null) {
075            formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
076        } else {
077            formatter =
078                    DateFormat.getDateInstance(
079                            DateFormat.SHORT,
080                            Locale.getDefault());
081        }
082
083        formatter.setLenient(false);
084
085        try {
086            formatter.parse(value);
087        } catch (final ParseException e) {
088            return false;
089        }
090
091        return true;
092    }
093
094    /**
095     * <p>Checks if the field is a valid date.  The pattern is used with
096     * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
097     * length will be checked so '2/12/1999' will not pass validation with
098     * the format 'MM/dd/yyyy' because the month isn't two digits.
099     * The setLenient method is set to {@code false} for all.</p>
100     *
101     * @param value The value validation is being performed on.
102     * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
103     * @param strict Whether or not to have an exact match of the datePattern.
104     * @return true if the date is valid.
105     */
106    public boolean isValid(final String value, final String datePattern, final boolean strict) {
107
108        if (value == null
109                || datePattern == null
110                || datePattern.isEmpty()) {
111
112            return false;
113        }
114
115        final SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
116        formatter.setLenient(false);
117
118        try {
119            formatter.parse(value);
120        } catch (final ParseException e) {
121            return false;
122        }
123
124        if (strict && datePattern.length() != value.length()) {
125            return false;
126        }
127
128        return true;
129    }
130
131}