Class DateTimeConverter
- java.lang.Object
-
- jakarta.faces.convert.DateTimeConverter
-
- All Implemented Interfaces:
PartialStateHolder,StateHolder,Converter
public class DateTimeConverter extends Object implements Converter, PartialStateHolder
Converterimplementation forjava.util.Datevalues.The
getAsObject()method parses a String into ajava.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
localeproperty is not null, use thatLocalefor managing parsing. Otherwise, use theLocalefrom theUIViewRoot. - If a
patternhas been specified, its syntax must conform the rules specified byjava.text.SimpleDateFormatorjava.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, andtimeStyleproperties will be ignored, unless the value oftypeis one of thejava.timespecific values listed insetType(java.lang.String). In this case,DateTimeFormatter.ofPattern(String, Locale)must be called, passing the value ofpatternas the first argument and the currentLocaleas the second argument, and this formatter must be used to parse the incoming value. - If a
patternhas not been specified, parsing will be based on thetypeproperty, which expects a date value, a time value, both, or one of several values specific to classes injava.timeas listed insetType(java.lang.String). Any date and time values included will be parsed in accordance to the styles specified bydateStyleandtimeStyle, respectively. - If a
timezonehas been specified, it must be passed to the underlyingDateFormatinstance. 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 typejava.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
localeproperty is not null, use thatLocalefor managing formatting. Otherwise, use theLocalefrom theUIViewRoot. - If a
timezonehas been specified, it must be passed to the underlyingDateFormatinstance. Otherwise the "GMT" timezone is used. - If a
patternhas been specified, its syntax must conform the rules specified byjava.text.SimpleDateFormatorjava.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, andtimeStyleproperties will be ignored, unless the value oftypeis one of thejava.timespecific values listed insetType(java.lang.String). In this case,DateTimeFormatter.ofPattern(String, Locale)must be called, passing the value ofpatternas the first argument and the currentLocaleas the second argument, and this formatter must be used to format the outgoing value. - If a
patternhas not been specified, formatting will be based on thetypeproperty, 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 bydateStyleandtimeStyle, respectively.
-
-
Field Summary
Fields Modifier and Type Field Description static StringCONVERTER_IDThe standard converter id for this converter.static StringDATE_IDThe message identifier of theFacesMessageto be created if the conversion toDatefails.static StringDATETIME_IDThe message identifier of theFacesMessageto be created if the conversion toDateTimefails.static StringSTRING_IDThe message identifier of theFacesMessageto be created if the conversion of theDateTimevalue toStringfails.static StringTIME_IDThe message identifier of theFacesMessageto be created if the conversion toTimefails.-
Fields inherited from interface jakarta.faces.convert.Converter
DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description DateTimeConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearInitialState()Reset the PartialStateHolder to a non-delta tracking state.ObjectgetAsObject(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.StringgetAsString(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.StringgetDateStyle()Return the style to be used to format or parse dates.LocalegetLocale()Return theLocaleto be used when parsing or formatting dates and times.StringgetPattern()Return the format pattern to be used when formatting and parsing dates and times.StringgetTimeStyle()Return the style to be used to format or parse times.TimeZonegetTimeZone()Return theTimeZoneused to interpret a time value.StringgetType()Return the type of value to be formatted or parsed.booleaninitialStateMarked()Returntrueif delta state changes are being tracked, otherwisefalsebooleanisTransient()If true, the Object implementing this interface must not participate in state saving or restoring.voidmarkInitialState()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.voidrestoreState(FacesContext context, Object state)Perform any processing required to restore the state from the entries in the state Object.ObjectsaveState(FacesContext context)Gets the state of the instance as aSerializableObject.voidsetDateStyle(String dateStyle)Set the style to be used to format or parse dates.voidsetLocale(Locale locale)Set theLocaleto be used when parsing or formatting dates and times.voidsetPattern(String pattern)Set the format pattern to be used when formatting and parsing dates and times.voidsetTimeStyle(String timeStyle)Set the style to be used to format or parse times.voidsetTimeZone(TimeZone timeZone)Set theTimeZoneused to interpret a time value.voidsetTransient(boolean transientFlag)Denotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.voidsetType(String type)Set the type of value to be formatted or parsed.
-
-
-
Field Detail
-
CONVERTER_ID
public static final String CONVERTER_ID
The standard converter id for this converter.
- See Also:
- Constant Field Values
-
DATE_ID
public static final String DATE_ID
The message identifier of the
FacesMessageto be created if the conversion toDatefails. 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 aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
TIME_ID
public static final String TIME_ID
The message identifier of the
FacesMessageto be created if the conversion toTimefails. 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 aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
DATETIME_ID
public static final String DATETIME_ID
The message identifier of the
FacesMessageto be created if the conversion toDateTimefails. 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 aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
STRING_ID
public static final String STRING_ID
The message identifier of the
FacesMessageto be created if the conversion of theDateTimevalue toStringfails. The message format string for this message may optionally include the following placeholders:{0}relaced by the unconverted value.{1}replaced by aStringwhose value is the label of the input component that produced this message.
- See Also:
- Constant Field Values
-
-
Method Detail
-
getDateStyle
public String 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
public void setDateStyle(String dateStyle)
Set the style to be used to format or parse dates. Valid values are
default,short,medium,long, andfull. An invalid value will cause aConverterExceptionwhengetAsObject()orgetAsString()is called.- Parameters:
dateStyle- The new style code
-
getLocale
public Locale getLocale()
Return the
Localeto be used when parsing or formatting dates and times. If not explicitly set, theLocalestored in theUIViewRootfor the current request is returned.- Returns:
- the
Locale
-
setLocale
public void setLocale(Locale locale)
Set the
Localeto be used when parsing or formatting dates and times. If set tonull, theLocalestored in theUIViewRootfor the current request will be utilized.- Parameters:
locale- The newLocale(ornull)
-
getPattern
public String getPattern()
Return the format pattern to be used when formatting and parsing dates and times.
- Returns:
- the pattern
-
setPattern
public void setPattern(String pattern)
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 aConverterExceptionwhengetAsObject()orgetAsString()is called.- Parameters:
pattern- The new format pattern
-
getTimeStyle
public String 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
public void setTimeStyle(String timeStyle)
Set the style to be used to format or parse times. Valid values are
default,short,medium,long, andfull. An invalid value will cause aConverterExceptionwhengetAsObject()orgetAsString()is called.- Parameters:
timeStyle- The new style code
-
getTimeZone
public TimeZone getTimeZone()
Return the
TimeZoneused to interpret a time value. If not explicitly set, the default time zone ofGMTreturned.- Returns:
- the
TimeZone
-
setTimeZone
public void setTimeZone(TimeZone timeZone)
Set the
TimeZoneused to interpret a time value.- Parameters:
timeZone- The new time zone
-
getType
public String getType()
Return the type of value to be formatted or parsed. If not explicitly set, the default type,
dateis returned.- Returns:
- the type
-
setType
public void setType(String type)
Set the type of value to be formatted or parsed. Valid values are
both,date,timelocalDate,localDateTime,localTime,offsetTime,offsetDateTime, orzonedDateTime. The values starting with "local", "offset" and "zoned" correspond to Java SE 8 Date Time API classes in packagejava.timewith the name derived by upper casing the first letter. For example,java.time.LocalDatefor the value"localDate". An invalid value will cause aConverterExceptionwhengetAsObject()orgetAsString()is called.- Parameters:
type- The new date style
-
getAsObject
public Object getAsObject(FacesContext context, UIComponent component, String value)
Description copied from interface:ConverterConvert 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:
getAsObjectin interfaceConverter- Parameters:
context-FacesContextfor the request being processedcomponent-UIComponentwith which this model object value is associatedvalue- String value to be converted (may benull)- Returns:
nullif the value to convert isnull, otherwise the result of the conversion- Throws:
ConverterException- if conversion cannot be successfully performedNullPointerException- ifcontextorcomponentisnull
-
getAsString
public String getAsString(FacesContext context, UIComponent component, Object value)
Description copied from interface:ConverterConvert 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:
getAsStringin interfaceConverter- Parameters:
context-FacesContextfor the request being processedcomponent-UIComponentwith 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- ifcontextorcomponentisnull
-
saveState
public Object saveState(FacesContext context)
Description copied from interface:StateHolderGets the state of the instance as a
SerializableObject.If the class that implements this interface has references to instances that implement StateHolder (such as a
UIComponentwith 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 theStateManagerThis method must not alter the state of the implementing object. In other words, after executing this code:
Object state = component.saveState(facesContext);componentshould be the same as before executing it.The return from this method must be
Serializable- Specified by:
saveStatein interfaceStateHolder- Parameters:
context- the Faces context.- Returns:
- the saved state.
-
restoreState
public void restoreState(FacesContext context, Object state)
Description copied from interface:StateHolderPerform 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
UIComponentwith 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
stateargument isnull, take no action and return.- Specified by:
restoreStatein interfaceStateHolder- Parameters:
context- the Faces context.state- the state.
-
isTransient
public boolean isTransient()
Description copied from interface:StateHolderIf true, the Object implementing this interface must not participate in state saving or restoring.
- Specified by:
isTransientin interfaceStateHolder- Returns:
trueif transient,falseotherwise.
-
setTransient
public void setTransient(boolean transientFlag)
Description copied from interface:StateHolderDenotes whether or not the Object implementing this interface must or must not participate in state saving or restoring.
- Specified by:
setTransientin interfaceStateHolder- Parameters:
transientFlag- boolean passtrueif this Object will not participate in state saving or restoring, otherwise passfalse.
-
markInitialState
public void markInitialState()
Description copied from interface:PartialStateHolderThe 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()returnstruefrom the timemarkInitialState()is called untilPartialStateHolder.clearInitialState()is called, after which timeinitialStateMarked()must returnfalse. Also, during the time that the instance returnstruefrominitialStateMarked(), the implementation must return only the state that has changed in its implementation ofStateHolder.saveState(jakarta.faces.context.FacesContext).- Specified by:
markInitialStatein interfacePartialStateHolder
-
initialStateMarked
public boolean initialStateMarked()
Description copied from interface:PartialStateHolderReturn
trueif delta state changes are being tracked, otherwisefalse- Specified by:
initialStateMarkedin interfacePartialStateHolder- Returns:
trueif the initial state is marked,falseotherwise.
-
clearInitialState
public void clearInitialState()
Description copied from interface:PartialStateHolderReset the PartialStateHolder to a non-delta tracking state.
- Specified by:
clearInitialStatein interfacePartialStateHolder
-
-