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