|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object
javax.faces.convert.DateTimeConverter
org.apache.myfaces.trinidad.convert.DateTimeConverter
@JSFConverter(configExcluded=true)
public class DateTimeConverter
Converter implementation for java.util.Date values. Converts an strings to and from java.util.Date objects.
The converter has additonal features than standard JSF DateTimeConverter. New dateStyle shortish has been introduced. Shortish is identical to short but forces the year to be a full four digits. If dateStyle is not set, then dateStyle defaults to shortish.
Timezone can be set per web-app in trinidad-config.xml configuration file. If timeZone is not set on the converter, then timezone will be defaulted to the value set in trinidad-config.xml configuration file. If it is not set in the configuration file, then it will be defaulted to GMT.
The converter always allows a level of leniency while converting user input value to date to the following extent.
For example:
When pattern on the converter is set to "MMM/d/yyyy" the following inputs are tolerated as valid by the converter.
The detail part of faces message,for conversion errors can be customized by overriding the message associated with each CONVERT_[TYPE]_MESSAGE_ID.
The methods used for customizing the detail message associated with each id is given below:
CONVERT_DATE_MESSAGE_ID - setMessageDetailConvertDate(String)CONVERT_TIME_MESSAGE_ID - setMessageDetailConvertTime(String)CONVERT_BOTH_MESSAGE_ID - setMessageDetailConvertBoth(String)The custom messages can contain placeholders, which will be replaced with values as specified in its corresponding message id.
The getAsObject() method parses a String into a java.util.Date, according to the following algorithm:
null. Otherwise, trim leading and trailing whitespace before proceeding.null.locale property is not null, use that Locale for managing parsing. Otherwise, use the Locale from the UIViewRoot.pattern has been specified, its syntax must conform the rules specified by java.text.SimpleDateFormat. Such a pattern will be used to parse, and the type, dateStyle, and timeStyle properties will be ignored.pattern has not been specified, parsing will be based on the type property, which expects a date value, a time value, or both. Any date and time values included will be parsed in accordance to the styles specified by dateStyle and timeStyle, respectively.pattern or style and if secondaryPattern is set, re parsers based on the secondaryPattern. Syntax for secondaryPattern must conform to the rules specified by java.text.SimpleDateFormat.java.text.DateFormatThe getAsString() method expects a value of type java.util.Date (or a subclass), and creates a formatted String according to the following algorithm:
locale property is not null, use that Locale for managing formatting. Otherwise, use the Locale from the UIViewRoot.pattern has been specified, its syntax must conform the rules specified by java.text.SimpleDateFormat. Such a pattern will be used to format, and the type, dateStyle, and timeStyle properties will be ignored.pattern has not been specified, formatting will be based on the type property, which includes a date value, a time value, or both into the formatted String. Any date and time values included will be formatted in accordance to the styles specified by dateStyle and timeStyle, respectively.secondaryPattern even if set is never used for formatting to a StringCONVERT_DATE_MESSAGE_ID, CONVERT_TIME_MESSAGE_ID, CONVERT_BOTH_MESSAGE_ID, DateFormat, SimpleDateFormat| Field Summary | |
|---|---|
static String |
CONVERT_BOTH_MESSAGE_IDThe message identifier of the FacesMessage to be created if the value cannot be converted to a date when type is set to 'both' and pattern is either null or not set. |
static String |
CONVERT_DATE_MESSAGE_IDThe message identifier of the FacesMessage to be created if the value cannot be converted to a date, when pattern is null or not set and type is set to 'date'. |
static String |
CONVERT_TIME_MESSAGE_IDThe message identifier of the FacesMessage to be created if the value cannot be converted date time object, when type is set to 'time' and pattern is null or not set. |
static String |
CONVERTER_IDStandard converter id for this converter. |
| Fields inherited from class javax.faces.convert.DateTimeConverter |
|---|
DATE_ID, DATETIME_ID, STRING_ID, TIME_ID |
| Constructor Summary | |
|---|---|
DateTimeConverter()Creates a DateTimeConverter |
|
DateTimeConverter(String pattern)Creates a DateTimeConverter with the specified SimpleDateFormat format pattern |
|
DateTimeConverter(String pattern, String secondaryPattern)Creates a DateTimeConverter with the specified SimpleDateFormat format pattern and a secondary pattern. |
|
| Method Summary | |
|---|---|
boolean |
equals(Object object)Compares this DateTimeConverter with the specified Object for equality. |
Object |
getAsObject(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, String value)Convert the specified string value, which is associated with the specified UIComponent, into a java.util.Date object based on the values set. |
String |
getAsString(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, Object value)Convert the model Date object value, into a String based on the pattern or styles. |
protected Date |
getDate(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component)Gets the existing date from the component. |
protected DateFormat |
getDateFormat(javax.faces.context.FacesContext context, String pattern, boolean forParsing, Date targetDate) |
String |
getDateStyle()Return the style to be used to format or parse dates. |
protected String |
getExample(javax.faces.context.FacesContext context) |
protected TimeZone |
getFormattingTimeZone(TimeZone tZone)Returns the TimeZone that will be set on DateFormat for formatting and parsing the dates. |
protected TimeZone |
getFormattingTimeZone(TimeZone tZone, Date targetDate)Returns the timeZone for formatting and parsing the date. |
String |
getHintBoth()Return custom hintBoth message. |
String |
getHintDate()Return custom hintDate message. |
String |
getHintTime()Return custom hintTime message. |
Locale |
getLocale()Return the Locale that was set. |
String |
getMessageDetailConvertBoth()Return custom detail error message that was set for creating FacesMessage, for values that cannot be converted to Date when type is set to 'both'. |
String |
getMessageDetailConvertDate()Return custom detail error message that was set for creating FacesMessage, for values that cannot be converted to Date when pattern / secondary pattern is set or when type is set to 'date'. |
String |
getMessageDetailConvertTime()Return custom detail error message that was set for creating FacesMessage, for values that cannot be converted to Date when type is set to 'time'. |
protected Object |
getMessagePattern(javax.faces.context.FacesContext context, String key, Object[] params, javax.faces.component.UIComponent component) |
protected javax.faces.application.FacesMessage |
getParseErrorMessage(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, String pattern, Object[] params) |
String |
getPattern()Return the format pattern to be used when formatting and parsing dates and times. |
String |
getSecondaryPattern()Return the secondary pattern used to parse string when parsing by pattern or style fails. |
String |
getTimeStyle()Return the style to be used to format or parse times. |
TimeZone |
getTimeZone()Return the TimeZone that is used to interpret a time value. |
String |
getType()Return the type of value to be formatted or parsed. |
javax.faces.el.ValueBinding |
getValueBinding(String name)Deprecated. |
javax.el.ValueExpression |
getValueExpression(String name)Return the ValueExpression used to calculate the value for the specified attribute name, if any. |
protected String |
getViolationMessageKey(String pattern) |
int |
hashCode()Returns the hash code for this Converter. |
boolean |
isDisabled()Return whether it is disabled. |
boolean |
isTransient() |
void |
restoreState(javax.faces.context.FacesContext context, Object state) |
Object |
saveState(javax.faces.context.FacesContext context) |
void |
setDateStyle(String dateStyle)Set the style to be used to format or parse dates. |
void |
setDisabled(boolean isDisabled)Set the value to property disabled. |
void |
setHintBoth(String hintBoth)Custom hintBoth message. |
void |
setHintDate(String hintDate)Custom hintDate message. |
void |
setHintTime(String hintTime)Custom hintTime message. |
void |
setLocale(Locale locale)Set the Locale to be used when parsing or formatting dates and times. |
void |
setMessageDetailConvertBoth(String convertBothMessageDetail)Custom error message to be used, for creating detail part of the FacesMessage, for date-time based value that cannot be converted to Date when type is set to 'both'. |
void |
setMessageDetailConvertDate(String convertDateMessageDetail)Custom error message to be used, for creating detail part of the FacesMessage, for values that cannot be converted to Date when the pattern / secondary pattern is set or when type is set to 'date'. |
void |
setMessageDetailConvertTime(String convertTimeMessageDetail)Custom error message to be used, for creating detail part of the FacesMessage, for time based value that cannot be converted to date when type is set to 'time'. |
void |
setPattern(String pattern)Set the format pattern to be used when formatting and parsing dates and times. |
void |
setSecondaryPattern(String secondaryPattern)Second pattern which will be used to parse string in getAsObject if pattern or styles fail. |
void |
setTimeStyle(String timeStyle)Set the style to be used to format or parse times. |
void |
setTimeZone(TimeZone timeZone)Set the TimeZone used to interpret a time value. |
void |
setTransient(boolean isTransient) |
void |
setType(String type)Set the type of value to be formatted or parsed. |
void |
setValueBinding(String name, javax.faces.el.ValueBinding binding)Deprecated. |
void |
setValueExpression(String name, javax.el.ValueExpression expression)Set the ValueExpression used to calculate the value for the specified attribute if any. |
| Methods inherited from class javax.faces.convert.DateTimeConverter |
|---|
clearInitialState, initialStateMarked, markInitialState |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String CONVERTER_ID
Standard converter id for this converter.
public static final String CONVERT_DATE_MESSAGE_ID
The message identifier of the FacesMessage to be created if the value cannot be converted to a date, when pattern is null or not set and type is set to 'date'. Or when failures occurs when value cannot be converted to a date based on the pattern set. The message format string for this message may optionally include {0}, {1}, {4} placeholdes, which will be replaced by input value, component label and example date based on the pattern or based on the style when type is set to 'date'.
public static final String CONVERT_TIME_MESSAGE_ID
The message identifier of the FacesMessage to be created if the value cannot be converted date time object, when type is set to 'time' and pattern is null or not set. The message format string for this message may optionally include {0}, {1}, {4} placeholdes, which will be replaced by input value, component label and a time example, based on the timeStyle set in the converter.
public static final String CONVERT_BOTH_MESSAGE_ID
The message identifier of the FacesMessage to be created if the value cannot be converted to a date when type is set to 'both' and pattern is either null or not set. The message format string for this message may optionally include {0}, {1}, {4} placeholdes, which will be replaced by input value, component label and a date-time example, based on the dateStyle and timeStyle set in the converter.
| Constructor Detail |
|---|
public DateTimeConverter()
public DateTimeConverter(String pattern)
pattern - a primary pattern; this will be used to format and parser strings.
public DateTimeConverter(String pattern,
String secondaryPattern)
pattern - a primary pattern; this will be used to format and parser strings.secondaryPattern - a second pattern, which will be used as a second attempt to parse a string if the primary pattern or styles fail, but is never used for formatting strings.| Method Detail |
|---|
public Object getAsObject(javax.faces.context.FacesContext context,
javax.faces.component.UIComponent component,
String value)
Convert the specified string value, which is associated with the specified UIComponent, into a java.util.Date object based on the values set.
getAsObject in interface javax.faces.convert.ConvertergetAsObject in class javax.faces.convert.DateTimeConvertercontext - FacesContext for the request being processedcomponent - UIComponent with which this model object value is associatedvalue - String value to be converted (may be null)null if the value to convert is null, otherwise return a java.util.Date object.javax.faces.convert.ConverterException - if conversion cannot be successfully performedNullPointerException - if context or component is nullIllegalArgumentException - if the value is of type other than Date, String. The value can be null.
public String getAsString(javax.faces.context.FacesContext context,
javax.faces.component.UIComponent component,
Object value)
Convert the model Date object value, into a String based on the pattern or styles.
getAsString in interface javax.faces.convert.ConvertergetAsString in class javax.faces.convert.DateTimeConvertercontext - FacesContext for the request being processedcomponent - UIComponent with which this model object value is associatedvalue - Model object value to be converted (may be null)null, otherwise the result of the conversionjavax.faces.convert.ConverterException - if conversion cannot be successfully performedNullPointerException - if context or component is nullpublic void setMessageDetailConvertDate(String convertDateMessageDetail)
Custom error message to be used, for creating detail part of the FacesMessage, for values that cannot be converted to Date when the pattern / secondary pattern is set or when type is set to 'date'.
CONVERT_DATE_MESSAGE_IDconvertDateMessageDetail - custom error message.
@JSFProperty
public String getMessageDetailConvertDate()
Return custom detail error message that was set for creating FacesMessage, for values that cannot be converted to Date when pattern / secondary pattern is set or when type is set to 'date'.
setMessageDetailConvertDate(String)public void setMessageDetailConvertTime(String convertTimeMessageDetail)
Custom error message to be used, for creating detail part of the FacesMessage, for time based value that cannot be converted to date when type is set to 'time'.
CONVERT_TIME_MESSAGE_IDconvertTimeMessageDetail - custom error message.
@JSFProperty
public String getMessageDetailConvertTime()
Return custom detail error message that was set for creating FacesMessage, for values that cannot be converted to Date when type is set to 'time'.
setMessageDetailConvertTime(java.lang.String)public void setMessageDetailConvertBoth(String convertBothMessageDetail)
Custom error message to be used, for creating detail part of the FacesMessage, for date-time based value that cannot be converted to Date when type is set to 'both'.
CONVERT_BOTH_MESSAGE_IDconvertBothMessageDetail - custom error message.CONVERT_BOTH_MESSAGE_ID
@JSFProperty
public String getMessageDetailConvertBoth()
FacesMessage, for values that cannot be converted to Date when type is set to 'both'.setMessageDetailConvertBoth(java.lang.String)public void setHintDate(String hintDate)
Custom hintDate message.
Overrides default hint messagehintDate - Custom hint message.public String getHintDate()
Return custom hintDate message.
setHintDate(String)public void setHintTime(String hintTime)
Custom hintTime message.
Overrides default hint messagehintTime - Custom hint message.public String getHintTime()
Return custom hintTime message.
setHintTime(String)public void setHintBoth(String hintBoth)
Custom hintBoth message.
Overrides default hint messagehintBoth - Custom hint message.public String getHintBoth()
Return custom hintBoth message.
setHintBoth(String)
protected Date getDate(javax.faces.context.FacesContext context,
javax.faces.component.UIComponent component)
This implementation checks to see if the component is a ValueHolder, and calls getValue() and returns the result if it is a Date instance.
component - The component to get the existing date from.public void setLocale(Locale locale)
Set the Locale to be used when parsing or formatting dates and times. If set to null, the Locale stored in the UIViewRoot for the current request will be utilized.
setLocale in class javax.faces.convert.DateTimeConverterlocale - The new Locale (or null)
@JSFProperty
public Locale getLocale()
Return the Locale that was set. If not explicitly set, the Locale stored in the UIViewRoot for the current request is used during call to getAsObject and getAsString.
getLocale in class javax.faces.convert.DateTimeConverterpublic 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 a ConverterException when getAsObject() or getAsString() is called.
setPattern in class javax.faces.convert.DateTimeConverterpattern - The new format pattern
@JSFProperty
public String getPattern()
Return the format pattern to be used when formatting and parsing dates and times.
getPattern in class javax.faces.convert.DateTimeConverterpublic void setTimeZone(TimeZone timeZone)
Set the TimeZone used to interpret a time value.
setTimeZone in class javax.faces.convert.DateTimeConvertertimeZone - The new time zone
@JSFProperty
public TimeZone getTimeZone()
Return the TimeZone that is used to interpret a time value. If not explicitly set or if a null value is set, then during call to getAsObject and getAsString, the time zone set for the web-app is used. If time zone is not set for the web-app then the default time zone of GMT is used.
getTimeZone in class javax.faces.convert.DateTimeConverterpublic void setType(String type)
Set the type of value to be formatted or parsed. Valid values are both, date, or time. An invalid value will cause a IllegalStateException when getAsObject() or getAsString() is called.
setType in class javax.faces.convert.DateTimeConvertertype - The new date style
@JSFProperty(defaultValue="date")
public String getType()
Return the type of value to be formatted or parsed. If not explicitly set, the default type, date is returned.
getType in class javax.faces.convert.DateTimeConverterpublic void setDateStyle(String dateStyle)
Set the style to be used to format or parse dates. Valid values are default, shortish short, medium, long, and full. An invalid value will cause a IllegalStateException when getAsObject() or getAsString() is called.
setDateStyle in class javax.faces.convert.DateTimeConverterdateStyle - The new style code
@JSFProperty(defaultValue="shortish")
public String getDateStyle()
Return the style to be used to format or parse dates. If not set, the default value, shortish, is returned.
getDateStyle in class javax.faces.convert.DateTimeConvertersetDateStyle(java.lang.String)public void setTimeStyle(String timeStyle)
Set the style to be used to format or parse times. Valid values are default, short, medium, long, and full. An invalid value will cause a IllegalStateException when getAsObject() or getAsString() is called.
setTimeStyle in class javax.faces.convert.DateTimeConvertertimeStyle - The new style code
@JSFProperty(defaultValue="short")
public String getTimeStyle()
Return the style to be used to format or parse times. If not set, the default value, short, is returned.
getTimeStyle in class javax.faces.convert.DateTimeConverterpublic void setSecondaryPattern(String secondaryPattern)
Second pattern which will be used to parse string in getAsObject if pattern or styles fail. But is never used for formatting to string in getAsString().
secondaryPattern - a second pattern which will be used as a second attempt to parse a string if the primary pattern or styles fail, but is never used for formatting strings.
@JSFProperty
public String getSecondaryPattern()
Return the secondary pattern used to parse string when parsing by pattern or style fails.
public boolean isTransient()
isTransient in interface javax.faces.component.StateHolderisTransient in class javax.faces.convert.DateTimeConverterpublic void setTransient(boolean isTransient)
setTransient in interface javax.faces.component.StateHoldersetTransient in class javax.faces.convert.DateTimeConverterpublic Object saveState(javax.faces.context.FacesContext context)
saveState in interface javax.faces.component.StateHoldersaveState in class javax.faces.convert.DateTimeConverter
public void restoreState(javax.faces.context.FacesContext context,
Object state)
restoreState in interface javax.faces.component.StateHolderrestoreState in class javax.faces.convert.DateTimeConverter
public void setValueExpression(String name,
javax.el.ValueExpression expression)
Set the ValueExpression used to calculate the value for the specified attribute if any.
name - Name of the attribute for which to set a ValueExpressionexpression - The ValueExpression to set, or null to remove any currently set ValueExpressionNullPointerException - if name is nullIllegalArgumentException - if name is not a valid attribute of this converterpublic javax.el.ValueExpression getValueExpression(String name)
Return the ValueExpression used to calculate the value for the specified attribute name, if any.
name - Name of the attribute or property for which to retrieve a ValueExpressionNullPointerException - if name is nullIllegalArgumentException - if name is not a valid attribute of this converter
public void setValueBinding(String name,
javax.faces.el.ValueBinding binding)
Set the ValueBinding used to calculate the value for the specified attribute if any.
name - Name of the attribute for which to set a ValueBindingbinding - The ValueBinding to set, or null to remove any currently set ValueBindingNullPointerException - if name is nullIllegalArgumentException - if name is not a valid attribute of this converterpublic javax.faces.el.ValueBinding getValueBinding(String name)
Return the ValueBinding used to calculate the value for the specified attribute name, if any.
name - Name of the attribute or property for which to retrieve a ValueBindingNullPointerException - if name is nullIllegalArgumentException - if name is not a valid attribute of this converterpublic boolean equals(Object object)
Compares this DateTimeConverter with the specified Object for equality.
equals in class Objectobject - Object to which this DateTimeConverter is to be compared.public int hashCode()
Returns the hash code for this Converter.
hashCode in class Objectpublic void setDisabled(boolean isDisabled)
Set the value to property disabled. Default value is false.
isDisabled - true if it's disabled, false otherwise.public boolean isDisabled()
protected final DateFormat getDateFormat(javax.faces.context.FacesContext context,
String pattern,
boolean forParsing,
Date targetDate)
throws javax.faces.convert.ConverterException
javax.faces.convert.ConverterExceptionprotected TimeZone getFormattingTimeZone(TimeZone tZone)
protected TimeZone getFormattingTimeZone(TimeZone tZone,
Date targetDate)
protected final javax.faces.application.FacesMessage getParseErrorMessage(javax.faces.context.FacesContext context,
javax.faces.component.UIComponent component,
String pattern,
Object[] params)
protected final String getExample(javax.faces.context.FacesContext context)
protected Object getMessagePattern(javax.faces.context.FacesContext context,
String key,
Object[] params,
javax.faces.component.UIComponent component)
protected String getViolationMessageKey(String pattern)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
Copyright © 2001-2013 The Apache Software Foundation. All Rights Reserved.