background image

Using DateTimeConverter

<< Converting a Component's Value | Using NumberConverter >>
<< Converting a Component's Value | Using NumberConverter >>

Using DateTimeConverter

<h:inputText value=
"#{LoginBean.Age}" />
<f:converter converterId=
"Integer" />
</h:inputText>
Instead of using the converterId attribute, the converter tag can use the binding attribute.
The binding attribute must resolve to a bean property that accepts and returns an appropriate
Converter
instance. See
"Binding Converters, Listeners, and Validators to Backing Bean
Properties" on page 369
for more information.
Using DateTimeConverter
You can convert a component's data to a java.util.Date by nesting the convertDateTime tag
inside the component tag. The convertDateTime tag has several attributes that allow you to
specify the format and type of the data.
Table 11­5
lists the attributes.
Here is a simple example of a convertDateTime tag from the bookreceipt.jsp page:
<h:outputText id=
"shipDate" value="#{cashier.shipDate}">
<f:convertDateTime dateStyle=
"full" />
</h:outputText>
When binding the DateTime converter to a component, ensure that the backing bean property
to which the component is bound is of type java.util.Date. In the case of the preceding
example, cashier.shipDate must be of type java.util.Date.
Here is an example of a date and time that the preceding tag can display:
Saturday, Feb 22, 2003
You can also display the same date and time using this tag:
<h:outputText value=
"#{cashier.shipDate}">
<f:convertDateTime
pattern=
"EEEEEEEE, MMM dd, yyyy" />
</h:outputText>
If you want to display the example date in Spanish, you can use the locale attribute:
<h:inputText value=
"#{cashier.shipDate}">
<f:convertDateTime dateStyle=
"full"
locale=
"Locale.SPAIN"
timeStyle=
"long" type="both" />
</h:inputText>
This tag would display
sabado 23 de septiembre de 2006
Using the Standard Converters
The Java EE 5 Tutorial · September 2007
356