Class DateFormatter
java.lang.Object
org.springframework.format.datetime.DateFormatter
A formatter for
Date
types.
Supports the configuration of an explicit date time pattern, timezone, locale, and fallback date time patterns for lenient parsing.
- Since:
- 3.0
- Author:
- Keith Donald, Juergen Hoeller, Phillip Webb, Sam Brannen
- See Also:
-
Constructor Summary
ConstructorDescriptionCreate a new defaultDateFormatter
.DateFormatter
(String pattern) Create a newDateFormatter
for the given date time pattern. -
Method Summary
Modifier and TypeMethodDescriptionprotected DateFormat
getDateFormat
(Locale locale) Parse a text String to produce a T.Print the object of type T for display.void
setFallbackPatterns
(String... fallbackPatterns) Set additional patterns to use as a fallback in case parsing fails for the configured pattern, ISO format, style, or style pattern.void
setIso
(DateTimeFormat.ISO iso) Set the ISO format to use to format date values.void
setLenient
(boolean lenient) Specify whether parsing is to be lenient.void
setPattern
(String pattern) Set the pattern to use to format date values.void
Set the source of the configuration for thisDateFormatter
— for example, an instance of the@DateTimeFormat
annotation if such an annotation was used to configure thisDateFormatter
.void
setStyle
(int style) Set theDateFormat
style to use to format date values.void
setStylePattern
(String stylePattern) Set the two characters to use to format date values.void
setTimeZone
(TimeZone timeZone) Set theTimeZone
to normalize the date values into, if any.
-
Constructor Details
-
DateFormatter
public DateFormatter()Create a new defaultDateFormatter
. -
DateFormatter
Create a newDateFormatter
for the given date time pattern.
-
-
Method Details
-
setSource
Set the source of the configuration for thisDateFormatter
— for example, an instance of the@DateTimeFormat
annotation if such an annotation was used to configure thisDateFormatter
.The supplied source object will only be used for descriptive purposes by invoking its
toString()
method — for example, when generating an exception message to provide further context.- Parameters:
source
- the source of the configuration- Since:
- 5.3.5
-
setPattern
Set the pattern to use to format date values.If not specified, DateFormat's default style will be used.
-
setFallbackPatterns
Set additional patterns to use as a fallback in case parsing fails for the configured pattern, ISO format, style, or style pattern.- Parameters:
fallbackPatterns
- the fallback parsing patterns- Since:
- 5.3.5
- See Also:
-
setIso
Set the ISO format to use to format date values.- Parameters:
iso
- theDateTimeFormat.ISO
format- Since:
- 3.2
-
setStyle
public void setStyle(int style) Set theDateFormat
style to use to format date values.If not specified, DateFormat's default style will be used.
-
setStylePattern
Set the two characters to use to format date values.The first character is used for the date style; the second is used for the time style.
Supported characters:
- 'S' = Small
- 'M' = Medium
- 'L' = Long
- 'F' = Full
- '-' = Omitted
- Parameters:
stylePattern
- two characters from the set {"S", "M", "L", "F", "-"}- Since:
- 3.2
-
setTimeZone
Set theTimeZone
to normalize the date values into, if any. -
setLenient
public void setLenient(boolean lenient) Specify whether parsing is to be lenient. Default isfalse
.With lenient parsing, the parser may allow inputs that do not precisely match the format. With strict parsing, inputs must match the format exactly.
-
print
Description copied from interface:Printer
Print the object of type T for display. -
parse
Description copied from interface:Parser
Parse a text String to produce a T.- Specified by:
parse
in interfaceParser<Date>
- Parameters:
text
- the text stringlocale
- the current user locale- Returns:
- an instance of T
- Throws:
ParseException
- when a parse exception occurs in a java.text parsing library
-
getDateFormat
-