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