Top Description Fields Constructors Methods
org.springframework.format.datetime

public Class DateFormatter

extends Object
implements Formatter<Date>
Class Inheritance
All Implemented Interfaces
org.springframework.format.Formatter, org.springframework.format.Parser, org.springframework.format.Printer
Imports
java.text.DateFormat, .ParseException, .SimpleDateFormat, java.util.Collections, .Date, .EnumMap, .Locale, .Map, .TimeZone, org.springframework.format.Formatter, org.springframework.format.annotation.DateTimeFormat, .DateTimeFormat.ISO, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils, .StringUtils

A formatter for java.util.Date types.

Supports the configuration of an explicit date time pattern, timezone, locale, and fallback date time patterns for lenient parsing.

Authors
Keith Donald, Juergen Hoeller, Phillip Webb, Sam Brannen
Since
3.0
See Also
SimpleDateFormat

Field Summary

Modifier and TypeField and Description
private String[]
private DateTimeFormat.ISO
private static final Map<DateTimeFormat.ISO, String>
private boolean
private String
private Object
private int
private String
private TimeZone
private static final TimeZone

Constructor Summary

AccessConstructor and Description
public
DateFormatter()

Create a new default DateFormatter.

public
DateFormatter(String pattern)

Create a new DateFormatter for the given date time pattern.

Method Summary

Modifier and TypeMethod and Description
private DateFormat
private DateFormat
protected DateFormat
private int
public Date
parse(String
the text string
text
,
Locale
the current user locale
locale
)

Implements org.springframework.format.Parser.parse.

Parse a text String to produce a T.

public void
setFallbackPatterns(String...
the fallback parsing patterns
fallbackPatterns
)

Set additional patterns to use as a fallback in case parsing fails for the configured pattern, ISO format, style, or style pattern.

public void
setIso(DateTimeFormat.ISO
the ISO format
iso
)

Set the ISO format to use to format date values.

public void
setLenient(boolean lenient)

Specify whether parsing is to be lenient.

public void
setPattern(String pattern)

Set the pattern to use to format date values.

public void
setSource(Object
the source of the configuration
source
)

Set the source of the configuration for this DateFormatter — for example, an instance of the @DateTimeFormat annotation if such an annotation was used to configure this DateFormatter.

public void
setStyle(int style)

Set the DateFormat style to use to format date values.

public void
setStylePattern(String
two characters from the set {"S", "M", "L", "F", "-"}
stylePattern
)

Set the two characters to use to format date values.

public void
setTimeZone(TimeZone timeZone)

Set the TimeZone to normalize the date values into, if any.

private IllegalStateException
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fallbackPatternsback to summary
private String[] fallbackPatterns
Annotations
@Nullable
isoback to summary
private DateTimeFormat.ISO iso
Annotations
@Nullable
ISO_PATTERNSback to summary
private static final Map<DateTimeFormat.ISO, String> ISO_PATTERNS
lenientback to summary
private boolean lenient
patternback to summary
private String pattern
Annotations
@Nullable
sourceback to summary
private Object source
Annotations
@Nullable
styleback to summary
private int style
stylePatternback to summary
private String stylePattern
Annotations
@Nullable
timeZoneback to summary
private TimeZone timeZone
Annotations
@Nullable
UTCback to summary
private static final TimeZone UTC

Constructor Detail

DateFormatterback to summary
public DateFormatter()

Create a new default DateFormatter.

DateFormatterback to summary
public DateFormatter(String pattern)

Create a new DateFormatter for the given date time pattern.

Method Detail

configureDateFormatback to summary
private DateFormat configureDateFormat(DateFormat dateFormat)
createDateFormatback to summary
private DateFormat createDateFormat(Locale locale)
getDateFormatback to summary
protected DateFormat getDateFormat(Locale locale)
getStylePatternForCharback to summary
private int getStylePatternForChar(int index)
parseback to summary
public Date parse(String text, Locale locale) throws ParseException

Implements org.springframework.format.Parser.parse.

Doc from org.springframework.format.Parser.parse.

Parse a text String to produce a T.

Parameters
text:String

the text string

locale:Locale

the current user locale

Returns:Date

an instance of T

Annotations
@Override
Exceptions
ParseException:
when a parse exception occurs in a java.text parsing library
setFallbackPatternsback to summary
public 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.

Parameters
fallbackPatterns:String[]

the fallback parsing patterns

Since
5.3.5
See Also
DateTimeFormat#fallbackPatterns()
setIsoback to summary
public void setIso(DateTimeFormat.ISO iso)

Set the ISO format to use to format date values.

Parameters
iso:DateTimeFormat.ISO

the ISO format

Since
3.2
setLenientback to summary
public void setLenient(boolean lenient)

Specify whether parsing is to be lenient. Default is false.

With lenient parsing, the parser may allow inputs that do not precisely match the format. With strict parsing, inputs must match the format exactly.

setPatternback to summary
public void setPattern(String pattern)

Set the pattern to use to format date values.

If not specified, DateFormat's default style will be used.

setSourceback to summary
public void setSource(Object source)

Set the source of the configuration for this DateFormatter — for example, an instance of the @DateTimeFormat annotation if such an annotation was used to configure this DateFormatter.

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:Object

the source of the configuration

Since
5.3.5
setStyleback to summary
public void setStyle(int style)

Set the DateFormat style to use to format date values.

If not specified, DateFormat's default style will be used.

See Also
DateFormat#DEFAULT, DateFormat#SHORT, DateFormat#MEDIUM, DateFormat#LONG, DateFormat#FULL
setStylePatternback to summary
public void setStylePattern(String stylePattern)

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
This method mimics the styles supported by Joda-Time.
Parameters
stylePattern:String

two characters from the set {"S", "M", "L", "F", "-"}

Since
3.2
setTimeZoneback to summary
public void setTimeZone(TimeZone timeZone)

Set the TimeZone to normalize the date values into, if any.

unsupportedStylePatternExceptionback to summary
private IllegalStateException unsupportedStylePatternException()