O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Formatting Dates and Numbers

One thing the inhabitants of this planet have a hard time agreeing on is how to write dates and numbers. The order of the month, the day, and the year; if the numeric value or the name should be used for the month; what character to use to separate the fractional part of a number; all of these details differ between countries, even between countries that speak the same language. And even though these details may seem picky, using an unfamiliar format can cause a great deal of confusion. For instance, if you ask for something to be done by 5/2, an American thinks you mean May 2 while a Swede believes that it’s due by February 5.

Java provides two classes, named java.text.NumberFormat and java.text.DateFormat, for formatting numbers and dates appropriately for a specific locale, and the standard JSF converters for date/time and numeric values use these classes.

Example 11-2 shows a localized version of the menu area page with an added feature: it displays the current date on the right.

Example 11-2. Internationalized menu area page (expense/stage2/menuArea.jsp)
<%@ page contentType="text/html" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<jsp:useBean id="now" scope="request" class="java.util.Date" /> <f:view locale="#{userProfile.locale}"> <f:loadBundle basename="labels" var="labels" /> <h:form> <table cellpadding="0" cellspacing="0" width="100%"> <tr> <td> <h:commandButton value="#{labels.newButtonLabel}" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required