org.apache.commons.cli2.validation
Class DateValidator

java.lang.Object
  extended by org.apache.commons.cli2.validation.DateValidator
All Implemented Interfaces:
Validator

public class DateValidator
extends Object
implements Validator

The DateValidator validates the argument values are date or time value(s). The following example shows how to validate that an argument value(s) is a Date of the following type: d/M/yy (see DateFormat).

 DateFormat date = new SimpleDateFormat("d/M/yy");
 ...
 ArgumentBuilder builder = new ArgumentBuilder();
 Argument dateFormat =
     builder.withName("date");
            .withValidator(new DateValidator(dateFormat));
 
The following example shows how to validate that an argument value(s) is a time of the following type: HH:mm:ss (see DateFormat).
 DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
 ...
 ArgumentBuilder builder = new ArgumentBuilder();
 Argument time =
     builder.withName("time");
            .withValidator(new DateValidator(timeFormat));
 

Author:
John Keyes
See Also:
DateFormat

Constructor Summary
DateValidator()
          Creates a Validator for the default date/time format
DateValidator(DateFormat format)
          Creates a Validator for the specified DateFormat.
DateValidator(List formats)
          Creates a Validator for the List of specified DateFormats.
 
Method Summary
static DateValidator getDateInstance()
          Creates a Validator for dates.
static DateValidator getDateTimeInstance()
          Creates a Validator for date/times
 DateFormat[] getFormats()
          Gets the date formats permitted.
 Date getMaximum()
          Returns the maximum date permitted.
 Date getMinimum()
          Returns the minimum date permitted.
static DateValidator getTimeInstance()
          Creates a Validator for times.
 boolean isLenient()
          Returns whether this validator uses lenient parsing.
 void setFormat(DateFormat format)
          Sets the date format permitted.
 void setFormats(DateFormat[] formats)
          Sets the date formats permitted.
 void setFormats(List formats)
          Sets the date formats permitted.
 void setLenient(boolean lenient)
          Sets whether this validator uses lenient parsing.
 void setMaximum(Date maximum)
          Sets the maximum Date to the specified value.
 void setMinimum(Date minimum)
          Sets the minimum Date to the specified value.
 void validate(List values)
          Validate each String value in the specified List against this instances permitted DateFormats.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateValidator

public DateValidator()
Creates a Validator for the default date/time format


DateValidator

public DateValidator(DateFormat format)
Creates a Validator for the specified DateFormat.

Parameters:
format - a DateFormat which dates must conform to

DateValidator

public DateValidator(List formats)
Creates a Validator for the List of specified DateFormats.

Parameters:
formats - a List of DateFormats which dates must conform to
Method Detail

getDateInstance

public static DateValidator getDateInstance()
Creates a Validator for dates.

Returns:
DateValidator a Validator for dates

getTimeInstance

public static DateValidator getTimeInstance()
Creates a Validator for times.

Returns:
DateValidator a Validator for times

getDateTimeInstance

public static DateValidator getDateTimeInstance()
Creates a Validator for date/times

Returns:
DateValidator a Validator for date/times

validate

public void validate(List values)
              throws InvalidArgumentException
Validate each String value in the specified List against this instances permitted DateFormats. If a value is valid then it's String value in the list is replaced with it's Date value.

Specified by:
validate in interface Validator
Parameters:
values - The values to validate.
Throws:
InvalidArgumentException - If any of the specified values are not valid.
See Also:
Validator.validate(java.util.List)

setLenient

public void setLenient(boolean lenient)
Sets whether this validator uses lenient parsing.

Parameters:
lenient - whether this validator uses lenient parsing

isLenient

public boolean isLenient()
Returns whether this validator uses lenient parsing.

Returns:
whether this validator uses lenient parsing

getMaximum

public Date getMaximum()
Returns the maximum date permitted.

Returns:
Date the maximum date permitted. If no maximum date has been specified then return null.

setMaximum

public void setMaximum(Date maximum)
Sets the maximum Date to the specified value.

Parameters:
maximum - the maximum Date permitted

getMinimum

public Date getMinimum()
Returns the minimum date permitted.

Returns:
Date the minimum date permitted. If no minimum date has been specified then return null.

setMinimum

public void setMinimum(Date minimum)
Sets the minimum Date to the specified value.

Parameters:
minimum - the minimum Date permitted

setFormat

public void setFormat(DateFormat format)
Sets the date format permitted.

Parameters:
format - the format to use

setFormats

public void setFormats(List formats)
Sets the date formats permitted.

Parameters:
formats - the List of DateFormats to use

setFormats

public void setFormats(DateFormat[] formats)
Sets the date formats permitted.

Parameters:
formats - the array of DateFormats to use

getFormats

public DateFormat[] getFormats()
Gets the date formats permitted.

Returns:
the permitted formats


Copyright © 2002-2010 Apache Software Foundation. All Rights Reserved.