1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.validator; 18 19 import java.text.DateFormat; 20 import java.text.ParseException; 21 import java.text.SimpleDateFormat; 22 import java.util.Locale; 23 24 /** 25 * <p>Perform date validations.</p> 26 * <p> 27 * This class is a Singleton; you can retrieve the instance via the 28 * getInstance() method. 29 * </p> 30 * 31 * @since 1.1 32 * @deprecated Use the new DateValidator, CalendarValidator or TimeValidator in the 33 * routines package. This class will be removed in a future release. 34 */ 35 @Deprecated 36 public class DateValidator { 37 38 /** 39 * Singleton instance of this class. 40 */ 41 private static final DateValidator DATE_VALIDATOR = new DateValidator(); 42 43 /** 44 * Returns the Singleton instance of this validator. 45 * @return A singleton instance of the DateValidator. 46 */ 47 public static DateValidator getInstance() { 48 return DATE_VALIDATOR; 49 } 50 51 /** 52 * Protected constructor for subclasses to use. 53 */ 54 protected DateValidator() { 55 } 56 57 /** 58 * <p>Checks if the field is a valid date. The <code>Locale</code> is 59 * used with <code>java.text.DateFormat</code>. The setLenient method 60 * is set to {@code false} for all.</p> 61 * 62 * @param value The value validation is being performed on. 63 * @param locale The locale to use for the date format, defaults to the default 64 * system default if null. 65 * @return true if the date is valid. 66 */ 67 public boolean isValid(final String value, final Locale locale) { 68 69 if (value == null) { 70 return false; 71 } 72 73 DateFormat formatter; 74 if (locale != null) { 75 formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale); 76 } else { 77 formatter = 78 DateFormat.getDateInstance( 79 DateFormat.SHORT, 80 Locale.getDefault()); 81 } 82 83 formatter.setLenient(false); 84 85 try { 86 formatter.parse(value); 87 } catch (final ParseException e) { 88 return false; 89 } 90 91 return true; 92 } 93 94 /** 95 * <p>Checks if the field is a valid date. The pattern is used with 96 * <code>java.text.SimpleDateFormat</code>. If strict is true, then the 97 * length will be checked so '2/12/1999' will not pass validation with 98 * the format 'MM/dd/yyyy' because the month isn't two digits. 99 * 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 }