public class DateTypeConverter extends java.lang.Object implements TypeConverter<java.util.Date>
A TypeConverter that aggressively attempts to convert a String to a java.util.Date object. Under the covers it uses DateFormat instances to do the heavy lifting, but since SimpleDateFormat is quite picky about its input a couple of measures are taken to improve our chances of parsing a Date.
First the String is pre-processed to replace commas, slashes, hyphens and periods with spaces and to collapse multiple white spaces into a single space. Then, to ensure that input like "Jan 1" and "3/19" are parseable a check is performed to see if there are only two segments in the input string (e.g. "Jan" and "1" but no "2007"). If that is the case then the current four digit year is appended to improve chances or parsing because a two segment date is not parsable by a DateFormat that expects a year.
Then an array of DateFormat instances are used in turn to attempt to parse the input. If any DateFormat succeeds and returns a Date, that Date will be returned as the result of the conversion. If all DateFormats fail, a validation error will be produced.
The set of formats is obtained from getFormatStrings(). The default set of formats used is constructed by taking the default SHORT, MEDIUM and LONG formats for the input locale (and replacing all non-space separator characters with spaces), and adding formats to obtain the following patterns:
This default set of formats can be changed by providing a different set of format strings in the Stripes resource bundle, or by subclassing and overriding getFormatStrings(). In all cases patterns should be specified using single spaces as separators instead of slashes, dashes or other characters.
The regular expression pattern used in the pre-process method can also be changed in the Stripes resource bundle, or by subclassing and overriding the getPreProcessPattern() method.
The keys used in the resource bundle to specify the format strings and the pre-process pattern are:
DateTypeConverter can also be overridden in order to change its behaviour. Subclasses can override the preProcessInput() method to change the pre-processing behavior if desired. Similarly, subclasses can override getDateFormats() to change how the DateFormat objects get constructed.
Modifier and Type | Field and Description |
---|---|
static java.lang.String[] |
formatStrings
The default set of date patterns used to parse dates with SimpleDateFormat.
|
static java.lang.String |
KEY_FORMAT_STRINGS
The key used to look up the localized format strings from the resource bundle.
|
static java.lang.String |
KEY_PRE_PROCESS_PATTERN
The key used to look up the pre-process pattern from the resource bundle.
|
static java.util.regex.Pattern |
PRE_PROCESS_PATTERN
A pattern used to pre-process Strings before the parsing attempt is made.
|
Constructor and Description |
---|
DateTypeConverter() |
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
checkAndAppendYear(java.lang.String input)
Checks to see how many 'parts' there are to the date (separated by spaces) and
if there are only two parts it adds the current year to the end by geting the
Locale specific year string from a Calendar instance.
|
java.util.Date |
convert(java.lang.String input,
java.lang.Class<? extends java.util.Date> targetType,
java.util.Collection<ValidationError> errors)
Attempts to convert a String to a Date object.
|
protected java.text.DateFormat[] |
getDateFormats()
Returns an array of DateFormat objects that will be used in sequence to try and parse the
date String.
|
protected java.lang.String[] |
getFormatStrings()
Returns an array of format strings that will be used, in order, to try and parse the date.
|
java.util.Locale |
getLocale() |
protected java.util.regex.Pattern |
getPreProcessPattern()
Returns the regular expression pattern used in the pre-process method.
|
protected java.lang.String |
getResourceString(java.lang.String key)
Convenience method to fetch a property from the resource bundle.
|
protected java.lang.String |
preProcessInput(java.lang.String input)
Pre-processes the input String to improve the chances of parsing it.
|
void |
setLocale(java.util.Locale locale)
Used by Stripes to set the input locale.
|
public static final java.util.regex.Pattern PRE_PROCESS_PATTERN
A pattern used to pre-process Strings before the parsing attempt is made. Since SimpleDateFormat strictly enforces that the separator characters in the input are the same as those in the pattern, this regular expression is used to remove commas, slashes, hyphens and periods from the input String (replacing them with spaces) and to collapse multiple white-space characters into a single space.
This pattern can be changed by providing a different value under the
'stripes.dateTypeConverter.preProcessPattern'
key in the resource
bundle. The default value is (?<!GMT)[\\s,-/\\.]+
.
public static final java.lang.String[] formatStrings
public static final java.lang.String KEY_FORMAT_STRINGS
public static final java.lang.String KEY_PRE_PROCESS_PATTERN
public void setLocale(java.util.Locale locale)
setLocale
in interface TypeConverter<java.util.Date>
locale
- the locale that the TypeConverter will be converting from.public java.util.Locale getLocale()
protected java.lang.String[] getFormatStrings()
protected java.text.DateFormat[] getDateFormats()
public java.util.Date convert(java.lang.String input, java.lang.Class<? extends java.util.Date> targetType, java.util.Collection<ValidationError> errors)
convert
in interface TypeConverter<java.util.Date>
input
- the String being convertedtargetType
- the Class representing the type of the property to which the return
value of the conversion will be assigned. In many cases this can be ignored as
converters will return a single type more often than not.errors
- an empty collection of validation errors that should be populated by the
converter for any errors that occur during validation that are user input related.protected java.util.regex.Pattern getPreProcessPattern()
(?<!GMT)[\\s,-/\\.]+
is used by default. The pattern is
used by preProcessInput() to replace all matches by single spaces.protected java.lang.String preProcessInput(java.lang.String input)
checkAndAppendYear(String)
to append the year to the date in case the date
is in a format like "12/25" which would otherwise fail to parse.protected java.lang.String checkAndAppendYear(java.lang.String input)
input
- the date string after the pre-process pattern has been run against itprotected java.lang.String getResourceString(java.lang.String key) throws java.util.MissingResourceException
java.util.MissingResourceException
? Copyright 2005-2006, Stripes Development Team.