public class DateFormatter extends Object implements Formatter<Date>
Date
types.
Supports the configuration of an explicit date time pattern, timezone, locale, and fallback date time patterns for lenient parsing.
Common ISO patterns for UTC instants are applied at millisecond precision.
Note that InstantFormatter
is recommended for flexible UTC parsing into a Instant
instead.
SimpleDateFormat
Constructor and Description |
---|
DateFormatter()
Create a new default
DateFormatter . |
DateFormatter(String pattern)
Create a new
DateFormatter for the given date time pattern. |
Modifier and Type | Method and Description |
---|---|
protected DateFormat |
getDateFormat(Locale locale) |
Date |
parse(String text,
Locale locale)
Parse a text String to produce a T.
|
String |
print(Date date,
Locale locale)
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 |
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 . |
void |
setStyle(int style)
Set the
DateFormat 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 the
TimeZone to normalize the date values into, if any. |
public DateFormatter()
DateFormatter
.public DateFormatter(String pattern)
DateFormatter
for the given date time pattern.public void setSource(Object source)
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.
source
- the source of the configurationpublic void setPattern(String pattern)
If not specified, DateFormat's default style will be used.
public void setFallbackPatterns(String... fallbackPatterns)
fallbackPatterns
- the fallback parsing patternsDateTimeFormat.fallbackPatterns()
public void setIso(DateTimeFormat.ISO iso)
iso
- the DateTimeFormat.ISO
formatpublic void setStyle(int style)
DateFormat
style to use to format date values.
If not specified, DateFormat's default style will be used.
public void setStylePattern(String stylePattern)
The first character is used for the date style; the second is used for the time style.
Supported characters:
stylePattern
- two characters from the set {"S", "M", "L", "F", "-"}public void setTimeZone(TimeZone timeZone)
TimeZone
to normalize the date values into, if any.public void setLenient(boolean lenient)
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.
public String print(Date date, Locale locale)
Printer
public Date parse(String text, Locale locale) throws ParseException
Parser
parse
in interface Parser<Date>
text
- the text stringlocale
- the current user localeParseException
- when a parse exception occurs in a java.text parsing libraryprotected DateFormat getDateFormat(Locale locale)