DateValidator.java

  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.routines;

  18. import java.text.DateFormat;
  19. import java.text.Format;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.Locale;
  23. import java.util.TimeZone;

  24. /**
  25.  * <p><b>Date Validation</b> and Conversion routines (<code>java.util.Date</code>).</p>
  26.  *
  27.  * <p>This validator provides a number of methods for validating/converting
  28.  *    a <code>String</code> date value to a <code>java.util.Date</code> using
  29.  *    <code>java.text.DateFormat</code> to parse either:</p>
  30.  *    <ul>
  31.  *       <li>using the default format for the default <code>Locale</code></li>
  32.  *       <li>using a specified pattern with the default <code>Locale</code></li>
  33.  *       <li>using the default format for a specified <code>Locale</code></li>
  34.  *       <li>using a specified pattern with a specified <code>Locale</code></li>
  35.  *    </ul>
  36.  *
  37.  * <p>For each of the above mechanisms, conversion method (i.e the
  38.  *    <code>validate</code> methods) implementations are provided which
  39.  *    either use the default <code>TimeZone</code> or allow the
  40.  *    <code>TimeZone</code> to be specified.</p>
  41.  *
  42.  * <p>Use one of the <code>isValid()</code> methods to just validate or
  43.  *    one of the <code>validate()</code> methods to validate and receive a
  44.  *    <i>converted</i> <code>Date</code> value.</p>
  45.  *
  46.  * <p>Implementations of the <code>validate()</code> method are provided
  47.  *    to create <code>Date</code> objects for different <i>time zones</i>
  48.  *    if the system default is not appropriate.</p>
  49.  *
  50.  * <p>Once a value has been successfully converted the following
  51.  *    methods can be used to perform various date comparison checks:</p>
  52.  *    <ul>
  53.  *       <li><code>compareDates()</code> compares the day, month and
  54.  *           year of two dates, returning 0, -1 or +1 indicating
  55.  *           whether the first date is equal, before or after the second.</li>
  56.  *       <li><code>compareWeeks()</code> compares the week and
  57.  *           year of two dates, returning 0, -1 or +1 indicating
  58.  *           whether the first week is equal, before or after the second.</li>
  59.  *       <li><code>compareMonths()</code> compares the month and
  60.  *           year of two dates, returning 0, -1 or +1 indicating
  61.  *           whether the first month is equal, before or after the second.</li>
  62.  *       <li><code>compareQuarters()</code> compares the quarter and
  63.  *           year of two dates, returning 0, -1 or +1 indicating
  64.  *           whether the first quarter is equal, before or after the second.</li>
  65.  *       <li><code>compareYears()</code> compares the
  66.  *           year of two dates, returning 0, -1 or +1 indicating
  67.  *           whether the first year is equal, before or after the second.</li>
  68.  *    </ul>
  69.  *
  70.  * <p>So that the same mechanism used for parsing an <i>input</i> value
  71.  *    for validation can be used to format <i>output</i>, corresponding
  72.  *    <code>format()</code> methods are also provided. That is you can
  73.  *    format either:</p>
  74.  *    <ul>
  75.  *       <li>using a specified pattern</li>
  76.  *       <li>using the format for a specified <code>Locale</code></li>
  77.  *       <li>using the format for the <i>default</i> <code>Locale</code></li>
  78.  *    </ul>
  79.  *
  80.  * @since 1.3.0
  81.  */
  82. public class DateValidator extends AbstractCalendarValidator {

  83.     private static final long serialVersionUID = -3966328400469953190L;

  84.     private static final DateValidator VALIDATOR = new DateValidator();

  85.     /**
  86.      * Gets the singleton instance of this validator.
  87.      * @return A singleton instance of the DateValidator.
  88.      */
  89.     public static DateValidator getInstance() {
  90.         return VALIDATOR;
  91.     }

  92.     /**
  93.      * Constructs a <i>strict</i> instance with <i>short</i>
  94.      * date style.
  95.      */
  96.     public DateValidator() {
  97.         this(true, DateFormat.SHORT);
  98.     }

  99.     /**
  100.      * Constructs an instance with the specified <i>strict</i>
  101.      * and <i>date style</i> parameters.
  102.      *
  103.      * @param strict {@code true} if strict
  104.      *        <code>Format</code> parsing should be used.
  105.      * @param dateStyle the date style to use for Locale validation.
  106.      */
  107.     public DateValidator(final boolean strict, final int dateStyle) {
  108.         super(strict, dateStyle, -1);
  109.     }

  110.     /**
  111.      * <p>Compare Dates (day, month and year - not time).</p>
  112.      *
  113.      * @param value The <code>Calendar</code> value to check.
  114.      * @param compare The <code>Calendar</code> to compare the value to.
  115.      * @param timeZone The Time Zone used to compare the dates, system default if null.
  116.      * @return Zero if the dates are equal, -1 if first
  117.      * date is less than the seconds and +1 if the first
  118.      * date is greater than.
  119.      */
  120.     public int compareDates(final Date value, final Date compare, final TimeZone timeZone) {
  121.         final Calendar calendarValue = getCalendar(value, timeZone);
  122.         final Calendar calendarCompare = getCalendar(compare, timeZone);
  123.         return compare(calendarValue, calendarCompare, Calendar.DATE);
  124.     }

  125.     /**
  126.      * <p>Compare Months (month and year).</p>
  127.      *
  128.      * @param value The <code>Date</code> value to check.
  129.      * @param compare The <code>Date</code> to compare the value to.
  130.      * @param timeZone The Time Zone used to compare the dates, system default if null.
  131.      * @return Zero if the months are equal, -1 if first
  132.      * parameter's month is less than the seconds and +1 if the first
  133.      * parameter's month is greater than.
  134.      */
  135.     public int compareMonths(final Date value, final Date compare, final TimeZone timeZone) {
  136.         final Calendar calendarValue = getCalendar(value, timeZone);
  137.         final Calendar calendarCompare = getCalendar(compare, timeZone);
  138.         return compare(calendarValue, calendarCompare, Calendar.MONTH);
  139.     }

  140.     /**
  141.      * <p>Compare Quarters (quarter and year).</p>
  142.      *
  143.      * @param value The <code>Date</code> value to check.
  144.      * @param compare The <code>Date</code> to compare the value to.
  145.      * @param timeZone The Time Zone used to compare the dates, system default if null.
  146.      * @return Zero if the months are equal, -1 if first
  147.      * parameter's quarter is less than the seconds and +1 if the first
  148.      * parameter's quarter is greater than.
  149.      */
  150.     public int compareQuarters(final Date value, final Date compare, final TimeZone timeZone) {
  151.         return compareQuarters(value, compare, timeZone, 1);
  152.     }

  153.     /**
  154.      * <p>Compare Quarters (quarter and year).</p>
  155.      *
  156.      * @param value The <code>Date</code> value to check.
  157.      * @param compare The <code>Date</code> to compare the value to.
  158.      * @param timeZone The Time Zone used to compare the dates, system default if null.
  159.      * @param monthOfFirstQuarter The  month that the first quarter starts.
  160.      * @return Zero if the quarters are equal, -1 if first
  161.      * parameter's quarter is less than the seconds and +1 if the first
  162.      * parameter's quarter is greater than.
  163.      */
  164.     public int compareQuarters(final Date value, final Date compare, final TimeZone timeZone, final int monthOfFirstQuarter) {
  165.         final Calendar calendarValue = getCalendar(value, timeZone);
  166.         final Calendar calendarCompare = getCalendar(compare, timeZone);
  167.         return super.compareQuarters(calendarValue, calendarCompare, monthOfFirstQuarter);
  168.     }

  169.     /**
  170.      * <p>Compare Weeks (week and year).</p>
  171.      *
  172.      * @param value The <code>Date</code> value to check.
  173.      * @param compare The <code>Date</code> to compare the value to.
  174.      * @param timeZone The Time Zone used to compare the dates, system default if null.
  175.      * @return Zero if the weeks are equal, -1 if first
  176.      * parameter's week is less than the seconds and +1 if the first
  177.      * parameter's week is greater than.
  178.      */
  179.     public int compareWeeks(final Date value, final Date compare, final TimeZone timeZone) {
  180.         final Calendar calendarValue = getCalendar(value, timeZone);
  181.         final Calendar calendarCompare = getCalendar(compare, timeZone);
  182.         return compare(calendarValue, calendarCompare, Calendar.WEEK_OF_YEAR);
  183.     }

  184.     /**
  185.      * <p>Compare Years.</p>
  186.      *
  187.      * @param value The <code>Date</code> value to check.
  188.      * @param compare The <code>Date</code> to compare the value to.
  189.      * @param timeZone The Time Zone used to compare the dates, system default if null.
  190.      * @return Zero if the years are equal, -1 if first
  191.      * parameter's year is less than the seconds and +1 if the first
  192.      * parameter's year is greater than.
  193.      */
  194.     public int compareYears(final Date value, final Date compare, final TimeZone timeZone) {
  195.         final Calendar calendarValue = getCalendar(value, timeZone);
  196.         final Calendar calendarCompare = getCalendar(compare, timeZone);
  197.         return compare(calendarValue, calendarCompare, Calendar.YEAR);
  198.     }

  199.     /**
  200.      * <p>Convert a <code>Date</code> to a <code>Calendar</code>.</p>
  201.      *
  202.      * @param value The date value to be converted.
  203.      * @return The converted <code>Calendar</code>.
  204.      */
  205.     private Calendar getCalendar(final Date value, final TimeZone timeZone) {
  206.         Calendar calendar;
  207.         if (timeZone != null) {
  208.             calendar = Calendar.getInstance(timeZone);
  209.         } else {
  210.             calendar = Calendar.getInstance();
  211.         }
  212.         calendar.setTime(value);
  213.         return calendar;

  214.     }

  215.     /**
  216.      * <p>Returns the parsed <code>Date</code> unchanged.</p>
  217.      *
  218.      * @param value The parsed <code>Date</code> object created.
  219.      * @param formatter The Format used to parse the value with.
  220.      * @return The parsed value converted to a <code>Calendar</code>.
  221.      */
  222.     @Override
  223.     protected Object processParsedValue(final Object value, final Format formatter) {
  224.         return value;
  225.     }

  226.     /**
  227.      * <p>Validate/convert a <code>Date</code> using the default
  228.      *    <code>Locale</code> and <code>TimeZone</code>.
  229.      *
  230.      * @param value The value validation is being performed on.
  231.      * @return The parsed <code>Date</code> if valid or {@code null}
  232.      *  if invalid.
  233.      */
  234.     public Date validate(final String value) {
  235.         return (Date) parse(value, (String) null, (Locale) null, (TimeZone) null);
  236.     }

  237.     /**
  238.      * <p>Validate/convert a <code>Date</code> using the specified
  239.      *    <code>Locale</code> and default <code>TimeZone</code>.
  240.      *
  241.      * @param value The value validation is being performed on.
  242.      * @param locale The locale to use for the date format, system default if null.
  243.      * @return The parsed <code>Date</code> if valid or {@code null} if invalid.
  244.      */
  245.     public Date validate(final String value, final Locale locale) {
  246.         return (Date) parse(value, (String) null, locale, (TimeZone) null);
  247.     }

  248.     /**
  249.      * <p>Validate/convert a <code>Date</code> using the specified
  250.      *    <code>Locale</code> and <code>TimeZone</code>.
  251.      *
  252.      * @param value The value validation is being performed on.
  253.      * @param locale The locale to use for the date format, system default if null.
  254.      * @param timeZone The Time Zone used to parse the date, system default if null.
  255.      * @return The parsed <code>Date</code> if valid or {@code null} if invalid.
  256.      */
  257.     public Date validate(final String value, final Locale locale, final TimeZone timeZone) {
  258.         return (Date) parse(value, (String) null, locale, timeZone);
  259.     }

  260.     /**
  261.      * <p>Validate/convert a <code>Date</code> using the specified
  262.      *    <i>pattern</i> and default <code>TimeZone</code>.
  263.      *
  264.      * @param value The value validation is being performed on.
  265.      * @param pattern The pattern used to validate the value against, or the
  266.      *        default for the <code>Locale</code> if {@code null}.
  267.      * @return The parsed <code>Date</code> if valid or {@code null} if invalid.
  268.      */
  269.     public Date validate(final String value, final String pattern) {
  270.         return (Date) parse(value, pattern, (Locale) null, (TimeZone) null);
  271.     }

  272.     /**
  273.      * <p>Validate/convert a <code>Date</code> using the specified pattern
  274.      *    and <code>Locale</code> and the default <code>TimeZone</code>.
  275.      *
  276.      * @param value The value validation is being performed on.
  277.      * @param pattern The pattern used to validate the value against, or the
  278.      *        default for the <code>Locale</code> if {@code null}.
  279.      * @param locale The locale to use for the date format, system default if null.
  280.      * @return The parsed <code>Date</code> if valid or {@code null} if invalid.
  281.      */
  282.     public Date validate(final String value, final String pattern, final Locale locale) {
  283.         return (Date) parse(value, pattern, locale, (TimeZone) null);
  284.     }

  285.     /**
  286.      * <p>Validate/convert a <code>Date</code> using the specified
  287.      *    pattern, and <code>Locale</code> and <code>TimeZone</code>.
  288.      *
  289.      * @param value The value validation is being performed on.
  290.      * @param pattern The pattern used to validate the value against, or the
  291.      *        default for the <code>Locale</code> if {@code null}.
  292.      * @param locale The locale to use for the date format, system default if null.
  293.      * @param timeZone The Time Zone used to parse the date, system default if null.
  294.      * @return The parsed <code>Date</code> if valid or {@code null} if invalid.
  295.      */
  296.     public Date validate(final String value, final String pattern, final Locale locale, final TimeZone timeZone) {
  297.         return (Date) parse(value, pattern, locale, timeZone);
  298.     }

  299.     /**
  300.      * <p>Validate/convert a <code>Date</code> using the specified
  301.      *    <i>pattern</i> and <code>TimeZone</code>.
  302.      *
  303.      * @param value The value validation is being performed on.
  304.      * @param pattern The pattern used to validate the value against, or the
  305.      *        default for the <code>Locale</code> if {@code null}.
  306.      * @param timeZone The Time Zone used to parse the date, system default if null.
  307.      * @return The parsed <code>Date</code> if valid or {@code null} if invalid.
  308.      */
  309.     public Date validate(final String value, final String pattern, final TimeZone timeZone) {
  310.         return (Date) parse(value, pattern, (Locale) null, timeZone);
  311.     }

  312.     /**
  313.      * <p>Validate/convert a <code>Date</code> using the specified
  314.      *    <code>TimeZone</code> and default <code>Locale</code>.
  315.      *
  316.      * @param value The value validation is being performed on.
  317.      * @param timeZone The Time Zone used to parse the date, system default if null.
  318.      * @return The parsed <code>Date</code> if valid or {@code null} if invalid.
  319.      */
  320.     public Date validate(final String value, final TimeZone timeZone) {
  321.         return (Date) parse(value, (String) null, (Locale) null, timeZone);
  322.     }

  323. }