Measuring, representing, formatting, and localizing date and time was always an important issue for developers. In this recipe, you will see how to get different formats for date and time using JSF standard converters. We will display a date/time in different formats and for different locales.
We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.
JSF provides a dedicated converter to accomplish tasks related to date and time, named converterDateTime
. This converter can be customized through a set of attributes listed in the following table:
Attribute name |
Description |
---|---|
|
Specifies whether to display the date, time, or both. |
|
Specifies the formatting style for the date portion of the string. Supported values are |
|
Specifies the formatting style for the time portion of the string. Valid options are |
|
Specifies the time zone for the date (For example, EST). By default GMT will be used. |
|
Specifies the locale to use for displaying the date (For example, Romania - |
|
Represents a date format pattern used to convert a number. |
Now, let's suppose that we have the current date (provided by a java.util.Date
instance). The next code will output this date using the f:converterDateTime
converter and the previously listed attributes:
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>Standard converters for date and time</title> </h:head> <h:body> <b><h:outputText value="-Formatting the current date and time-"/></b><br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime type="date" dateStyle="medium"/> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime type="date" dateStyle="full"/> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime type="time" timeStyle="full"/> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime type="date" pattern="dd/mm/yyyy"/> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime dateStyle="full" pattern="yyyy-mm-dd"/> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime dateStyle="full" pattern="yyyy.MM.dd 'at' HH:mm:ss z"/> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime dateStyle="full" pattern="h:mm a"/> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime dateStyle="long" timeZone="EST" type="both" /> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime locale="ro" timeStyle="long" type="both" dateStyle="full" /> </h:outputText> <br /> <h:outputText value="#{datetimeBean.currentdate}"> <f:convertDateTime locale="de" timeStyle="short" type="both" dateStyle="full" /> </h:outputText> </h:body> </html>
The datetimeBean
is listed next:
package datetime; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.util.Date; @ManagedBean @SessionScoped public class DatetimeBean { private Date currentdate = new Date(); public Date getCurrentdate(){ return this.currentdate; } public void setCurrentdate(Date currentdate){ this.currentdate=currentdate; } }
The output will be as follows:
-Formatting the current date and time-
Jun 15, 2009
Monday, June 15, 2009
11:14:53 AM GMT
15/14/2009
2009-14-15
2009.06.15 at 11:14:53 GMT
11:14 AM
June 15, 2009 6:14:53 AM
15 iunie 2009 11:14:53 GMT
Montag, 15. Juni 2009 11:14