org.springframework.beans.propertyeditors
public class: CustomDateEditor [javadoc |
source]
java.lang.Object
java.beans.PropertyEditorSupport
org.springframework.beans.propertyeditors.CustomDateEditor
All Implemented Interfaces:
PropertyEditor
Property editor for
java.util.Date,
supporting a custom
java.text.DateFormat.
This is not meant to be used as system PropertyEditor but rather
as locale-specific date editor within custom controller code,
parsing user-entered number strings into Date properties of beans
and rendering them in the UI form.
In web MVC code, this editor will typically be registered with
binder.registerCustomEditor calls in a custom
initBinder method.
Also see:
- java.util.Date
- java.text.DateFormat
- org.springframework.validation.DataBinder#registerCustomEditor
- org.springframework.web.servlet.mvc.BaseCommandController#initBinder
- author:
Juergen - Hoeller
- since:
28.04.2003 -
| Constructor: |
public CustomDateEditor(DateFormat dateFormat,
boolean allowEmpty) {
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.exactDateLength = -1;
}
Create a new CustomDateEditor instance, using the given DateFormat
for parsing and rendering.
The "allowEmpty" parameter states if an empty String should
be allowed for parsing, i.e. get interpreted as null value.
Otherwise, an IllegalArgumentException gets thrown in that case. Parameters:
dateFormat - DateFormat to use for parsing and rendering
allowEmpty - if empty strings should be allowed
|
public CustomDateEditor(DateFormat dateFormat,
boolean allowEmpty,
int exactDateLength) {
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.exactDateLength = exactDateLength;
}
Create a new CustomDateEditor instance, using the given DateFormat
for parsing and rendering.
The "allowEmpty" parameter states if an empty String should
be allowed for parsing, i.e. get interpreted as null value.
Otherwise, an IllegalArgumentException gets thrown in that case.
The "exactDateLength" parameter states that IllegalArgumentException gets
thrown if the String does not exactly match the length specified. This is useful
because SimpleDateFormat does not enforce strict parsing of the year part,
not even with setLenient(false). Without an "exactDateLength"
specified, the "01/01/05" would get parsed to "01/01/0005". Parameters:
dateFormat - DateFormat to use for parsing and rendering
allowEmpty - if empty strings should be allowed
exactDateLength - the exact expected length of the date String
|
| Method from org.springframework.beans.propertyeditors.CustomDateEditor Summary: |
|---|
|
getAsText, setAsText |
| Methods from java.beans.PropertyEditorSupport: |
|---|
|
addPropertyChangeListener, firePropertyChange, getAsText, getCustomEditor, getJavaInitializationString, getSource, getTags, getValue, isPaintable, paintValue, removePropertyChangeListener, setAsText, setSource, setValue, supportsCustomEditor |
| Method from org.springframework.beans.propertyeditors.CustomDateEditor Detail: |
public String getAsText() {
Date value = (Date) getValue();
return (value != null ? this.dateFormat.format(value) : "");
}
Format the Date as String, using the specified DateFormat. |
public void setAsText(String text) throws IllegalArgumentException {
if (this.allowEmpty && !StringUtils.hasText(text)) {
// Treat empty String as null value.
setValue(null);
}
else if (text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
throw new IllegalArgumentException(
"Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
}
else {
try {
setValue(this.dateFormat.parse(text));
}
catch (ParseException ex) {
IllegalArgumentException iae =
new IllegalArgumentException("Could not parse date: " + ex.getMessage());
iae.initCause(ex);
throw iae;
}
}
}
Parse the Date from the given text, using the specified DateFormat. |