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