Class DateTimeConverter
- All Implemented Interfaces:
PartialStateHolder
,StateHolder
,Converter
Converter
implementation for
java.util.Date
values.
The getAsObject()
method parses a String into a java.util.Date
, according to the following
algorithm:
- If the specified String is null, return a
null
. Otherwise, trim leading and trailing whitespace before proceeding. - If the specified String - after trimming - has a zero length, return
null
. - If the
locale
property is not null, use thatLocale
for managing parsing. Otherwise, use theLocale
from theUIViewRoot
. - If a
pattern
has been specified, its syntax must conform the rules specified byjava.text.SimpleDateFormat
orjava.time.format.DateTimeFormatter
. Which of these two formatters is used depends on the value oftype
. Such a pattern will be used to parse, and thetype
,dateStyle
, andtimeStyle
properties will be ignored, unless the value oftype
is one of thejava.time
specific values listed insetType(java.lang.String)
. In this case,DateTimeFormatter.ofPattern(String, Locale)
must be called, passing the value ofpattern
as the first argument and the currentLocale
as the second argument, and this formatter must be used to parse the incoming value. - If a
pattern
has not been specified, parsing will be based on thetype
property, which expects a date value, a time value, both, or one of several values specific to classes injava.time
as listed insetType(java.lang.String)
. Any date and time values included will be parsed in accordance to the styles specified bydateStyle
andtimeStyle
, respectively. - If a
timezone
has been specified, it must be passed to the underlyingDateFormat
instance. Otherwise the "GMT" timezone is used. - In all cases, parsing must be non-lenient; the given string must strictly adhere to the parsing format.
The getAsString()
method expects a value of type java.util.Date
(or a subclass), and
creates a formatted String according to the following algorithm:
- If the specified value is null, return a zero-length String.
- If the specified value is a String, return it unmodified.
- If the
locale
property is not null, use thatLocale
for managing formatting. Otherwise, use theLocale
from theUIViewRoot
. - If a
timezone
has been specified, it must be passed to the underlyingDateFormat
instance. Otherwise the "GMT" timezone is used. - If a
pattern
has been specified, its syntax must conform the rules specified byjava.text.SimpleDateFormat
orjava.time.format.DateTimeFormatter
. Which of these two formatters is used depends on the value oftype
. Such a pattern will be used to format, and thetype
,dateStyle
, andtimeStyle
properties will be ignored, unless the value oftype
is one of thejava.time
specific values listed insetType(java.lang.String)
. In this case,DateTimeFormatter.ofPattern(String, Locale)
must be called, passing the value ofpattern
as the first argument and the currentLocale
as the second argument, and this formatter must be used to format the outgoing value. - If a
pattern
has not been specified, formatting will be based on thetype
property, which includes a date value, a time value, both or into the formatted String. Any date and time values included will be formatted in accordance to the styles specified bydateStyle
andtimeStyle
, respectively.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The standard converter id for this converter.static final String
The message identifier of theFacesMessage
to be created if the conversion toDate
fails.static final String
The message identifier of theFacesMessage
to be created if the conversion toDateTime
fails.static final String
The message identifier of theFacesMessage
to be created if the conversion of theDateTime
value toString
fails.static final String
The message identifier of theFacesMessage
to be created if the conversion toTime
fails.Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Reset the PartialStateHolder to a non-delta tracking state.getAsObject
(FacesContext context, UIComponent component, String value) Convert the specified string value, which is associated with the specifiedUIComponent
, into a model data object that is appropriate for being stored during the Process Validations phase of the request processing lifecycle.getAsString
(FacesContext context, UIComponent component, Object value) Convert the specified model object value, which is associated with the specifiedUIComponent
, into a String that is suitable for being included in the response generated during the Render Response phase of the request processing lifeycle.Return the style to be used to format or parse dates.Return theLocale
to be used when parsing or formatting dates and times.Return the format pattern to be used when formatting and parsing dates and times.Return the style to be used to format or parse times.Return theTimeZone
used to interpret a time value.getType()
Return the type of value to be formatted or parsed.boolean
Returntrue
if delta state changes are being tracked, otherwisefalse
boolean
If true, the Object implementing this interface must not participate in state saving or restoring.void
The runtime must ensure that thePartialStateHolder.markInitialState()
method is called on each instance of this interface in the view at the appropriate time to indicate the component is in its initial state.void
restoreState
(FacesContext context, Object state) Perform any processing required to restore the state from the entries in the state Object.saveState
(FacesContext context) Gets the state of the instance as aSerializable
Object.void
setDateStyle
(String dateStyle) Set the style to be used to format or parse dates.void
Set theLocale
to be used when parsing or formatting dates and times.void
setPattern
(String pattern) Set the format pattern to be used when formatting and parsing dates and times.void
setTimeStyle
(String timeStyle) Set the style to be used to format or parse times.void
setTimeZone
(TimeZone timeZone) Set theTimeZone
used to interpret a time value.void
setTransient
(boolean transientFlag) Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.void
Set the type of value to be formatted or parsed.
-
Field Details
-
CONVERTER_ID
The standard converter id for this converter.
- See Also:
-
DATE_ID
The message identifier of the
FacesMessage
to be created if the conversion toDate
fails. The message format string for this message may optionally include the following placeholders:{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
-
TIME_ID
The message identifier of the
FacesMessage
to be created if the conversion toTime
fails. The message format string for this message may optionally include the following placeholders:{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
-
DATETIME_ID
The message identifier of the
FacesMessage
to be created if the conversion toDateTime
fails. The message format string for this message may optionally include the following placeholders:{0}
replaced by the unconverted value.{1}
replaced by an example value.{2}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
-
STRING_ID
The message identifier of the
FacesMessage
to be created if the conversion of theDateTime
value toString
fails. The message format string for this message may optionally include the following placeholders:{0}
relaced by the unconverted value.{1}
replaced by aString
whose value is the label of the input component that produced this message.
- See Also:
-
-
Constructor Details
-
DateTimeConverter
public DateTimeConverter()
-
-
Method Details
-
getDateStyle
Return the style to be used to format or parse dates. If not set, the default value,
default
, is returned.- Returns:
- the style
-
setDateStyle
Set the style to be used to format or parse dates. Valid values are
default
,short
,medium
,long
, andfull
. An invalid value will cause aConverterException
whengetAsObject()
orgetAsString()
is called.- Parameters:
dateStyle
- The new style code
-
getLocale
Return the
Locale
to be used when parsing or formatting dates and times. If not explicitly set, theLocale
stored in theUIViewRoot
for the current request is returned.- Returns:
- the
Locale
-
setLocale
Set the
Locale
to be used when parsing or formatting dates and times. If set tonull
, theLocale
stored in theUIViewRoot
for the current request will be utilized.- Parameters:
locale
- The newLocale
(ornull
)
-
getPattern
Return the format pattern to be used when formatting and parsing dates and times.
- Returns:
- the pattern
-
setPattern
Set the format pattern to be used when formatting and parsing dates and times. Valid values are those supported by
java.text.SimpleDateFormat
. An invalid value will cause aConverterException
whengetAsObject()
orgetAsString()
is called.- Parameters:
pattern
- The new format pattern
-
getTimeStyle
Return the style to be used to format or parse times. If not set, the default value,
default
, is returned.- Returns:
- the time style
-
setTimeStyle
Set the style to be used to format or parse times. Valid values are
default
,short
,medium
,long
, andfull
. An invalid value will cause aConverterException
whengetAsObject()
orgetAsString()
is called.- Parameters:
timeStyle
- The new style code
-
getTimeZone
Return the
TimeZone
used to interpret a time value. If not explicitly set, the default time zone ofGMT
returned.- Returns:
- the
TimeZone
-
setTimeZone
Set the
TimeZone
used to interpret a time value.- Parameters:
timeZone
- The new time zone
-
getType
Return the type of value to be formatted or parsed. If not explicitly set, the default type,
date
is returned.- Returns:
- the type
-
setType
Set the type of value to be formatted or parsed. Valid values are
both
,date
,time
localDate
,localDateTime
,localTime
,offsetTime
,offsetDateTime
, orzonedDateTime
. The values starting with "local", "offset" and "zoned" correspond to Java SE 8 Date Time API classes in packagejava.time
with the name derived by upper casing the first letter. For example,java.time.LocalDate
for the value"localDate"
. An invalid value will cause aConverterException
whengetAsObject()
orgetAsString()
is called.- Parameters:
type
- The new date style
-
getAsObject
Description copied from interface:Converter
Convert the specified string value, which is associated with the specified
UIComponent
, into a model data object that is appropriate for being stored during the Process Validations phase of the request processing lifecycle.- Specified by:
getAsObject
in interfaceConverter
- Parameters:
context
-FacesContext
for the request being processedcomponent
-UIComponent
with which this model object value is associatedvalue
- String value to be converted (may benull
)- Returns:
null
if the value to convert isnull
, otherwise the result of the conversion- Throws:
ConverterException
- if conversion cannot be successfully performedNullPointerException
- ifcontext
orcomponent
isnull
-
getAsString
Description copied from interface:Converter
Convert the specified model object value, which is associated with the specified
UIComponent
, into a String that is suitable for being included in the response generated during the Render Response phase of the request processing lifeycle.- Specified by:
getAsString
in interfaceConverter
- Parameters:
context
-FacesContext
for the request being processedcomponent
-UIComponent
with which this model object value is associatedvalue
- Model object value to be converted (may benull
)- Returns:
- a zero-length String if value is
null
, otherwise the result of the conversion - Throws:
ConverterException
- if conversion cannot be successfully performedNullPointerException
- ifcontext
orcomponent
isnull
-
saveState
Description copied from interface:StateHolder
Gets the state of the instance as a
Serializable
Object.If the class that implements this interface has references to instances that implement StateHolder (such as a
UIComponent
with event handlers, validators, etc.) this method must call theStateHolder.saveState(jakarta.faces.context.FacesContext)
method on all those instances as well. This method must not save the state of children and facets. That is done via theStateManager
This method must not alter the state of the implementing object. In other words, after executing this code:
Object state = component.saveState(facesContext);
component
should be the same as before executing it.The return from this method must be
Serializable
- Specified by:
saveState
in interfaceStateHolder
- Parameters:
context
- the Faces context.- Returns:
- the saved state.
-
restoreState
Description copied from interface:StateHolder
Perform any processing required to restore the state from the entries in the state Object.
If the class that implements this interface has references to instances that also implement StateHolder (such as a
UIComponent
with event handlers, validators, etc.) this method must call theStateHolder.restoreState(jakarta.faces.context.FacesContext, java.lang.Object)
method on all those instances as well.If the
state
argument isnull
, take no action and return.- Specified by:
restoreState
in interfaceStateHolder
- Parameters:
context
- the Faces context.state
- the state.
-
isTransient
public boolean isTransient()Description copied from interface:StateHolder
If true, the Object implementing this interface must not participate in state saving or restoring.
- Specified by:
isTransient
in interfaceStateHolder
- Returns:
true
if transient,false
otherwise.
-
setTransient
public void setTransient(boolean transientFlag) Description copied from interface:StateHolder
Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
- Specified by:
setTransient
in interfaceStateHolder
- Parameters:
transientFlag
- boolean passtrue
if this Object will not participate in state saving or restoring, otherwise passfalse
.
-
markInitialState
public void markInitialState()Description copied from interface:PartialStateHolder
The runtime must ensure that the
PartialStateHolder.markInitialState()
method is called on each instance of this interface in the view at the appropriate time to indicate the component is in its initial state. The implementor of the interface must ensure thatPartialStateHolder.initialStateMarked()
returnstrue
from the timemarkInitialState()
is called untilPartialStateHolder.clearInitialState()
is called, after which timeinitialStateMarked()
must returnfalse
. Also, during the time that the instance returnstrue
frominitialStateMarked()
, the implementation must return only the state that has changed in its implementation ofStateHolder.saveState(jakarta.faces.context.FacesContext)
.- Specified by:
markInitialState
in interfacePartialStateHolder
-
initialStateMarked
public boolean initialStateMarked()Description copied from interface:PartialStateHolder
Return
true
if delta state changes are being tracked, otherwisefalse
- Specified by:
initialStateMarked
in interfacePartialStateHolder
- Returns:
true
if the initial state is marked,false
otherwise.
-
clearInitialState
public void clearInitialState()Description copied from interface:PartialStateHolder
Reset the PartialStateHolder to a non-delta tracking state.
- Specified by:
clearInitialState
in interfacePartialStateHolder
-