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.
<%@ 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}" ...
Get JavaServer Faces now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.