Top Description Fields Constructors Methods
org.springframework.beans.propertyeditors

public Class CustomDateEditor

extends PropertyEditorSupport
Class Inheritance
Imports
java.beans.PropertyEditorSupport, java.text.DateFormat, .ParseException, java.util.Date, org.springframework.lang.Nullable, org.springframework.util.StringUtils

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.

Author
Juergen Hoeller
Since
28.04.2003
See Also
java.util.Date, java.text.DateFormat, org.springframework.validation.DataBinder#registerCustomEditor

Field Summary

Modifier and TypeField and Description
private final boolean
private final DateFormat
private final int

Constructor Summary

AccessConstructor and Description
public
CustomDateEditor(DateFormat
the DateFormat to use for parsing and rendering
dateFormat
,
boolean
if empty strings should be allowed
allowEmpty
)

Create a new CustomDateEditor instance, using the given DateFormat for parsing and rendering.

public
CustomDateEditor(DateFormat
the DateFormat to use for parsing and rendering
dateFormat
,
boolean
if empty strings should be allowed
allowEmpty
,
int
the exact expected length of the date String
exactDateLength
)

Create a new CustomDateEditor instance, using the given DateFormat for parsing and rendering.

Method Summary

Modifier and TypeMethod and Description
public String
getAsText()

Overrides java.beans.PropertyEditorSupport.getAsText.

Implements java.beans.PropertyEditor.getAsText.

Format the Date as String, using the specified DateFormat.

public void
setAsText(String
The string to be parsed.
text
)

Overrides java.beans.PropertyEditorSupport.setAsText.

Implements java.beans.PropertyEditor.setAsText.

Parse the Date from the given text, using the specified DateFormat.

Inherited from java.beans.PropertyEditorSupport:
addPropertyChangeListenerfirePropertyChangegetCustomEditorgetJavaInitializationStringgetSourcegetTagsgetValueisPaintablepaintValueremovePropertyChangeListenersetSourcesetValuesupportsCustomEditor

Field Detail

allowEmptyback to summary
private final boolean allowEmpty
dateFormatback to summary
private final DateFormat dateFormat
exactDateLengthback to summary
private final int exactDateLength

Constructor Detail

CustomDateEditorback to summary
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty)

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

the DateFormat to use for parsing and rendering

allowEmpty:boolean

if empty strings should be allowed

CustomDateEditorback to summary
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty, int 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". However, even with an "exactDateLength" specified, prepended zeros in the day or month part may still allow for a shorter year part, so consider this as just one more assertion that gets you closer to the intended date format.

Parameters
dateFormat:DateFormat

the DateFormat to use for parsing and rendering

allowEmpty:boolean

if empty strings should be allowed

exactDateLength:int

the exact expected length of the date String

Method Detail

getAsTextback to summary
public String getAsText()

Overrides java.beans.PropertyEditorSupport.getAsText.

Implements java.beans.PropertyEditor.getAsText.

Format the Date as String, using the specified DateFormat.

Returns:String

Doc from java.beans.PropertyEditorSupport.getAsText.

The property value as a string suitable for presentation to a human to edit.

Returns null if the value can't be expressed as a string.

If a non-null value is returned, then the PropertyEditor should be prepared to parse that string back in setAsText().

Annotations
@Override
setAsTextback to summary
public void setAsText(String text) throws IllegalArgumentException

Overrides java.beans.PropertyEditorSupport.setAsText.

Implements java.beans.PropertyEditor.setAsText.

Parse the Date from the given text, using the specified DateFormat.

Parameters
text:String

Doc from java.beans.PropertyEditorSupport.setAsText.

The string to be parsed.

Annotations
@Override