The Java EE 5 Tutorial

Formatting Tags

JSTL provides a set of tags for parsing and formatting locale-sensitive numbers and dates.

The formatNumber tag is used to output localized numbers. The following tag from tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookshowcart.jsp is used to display a localized price for a book.

<fmt:formatNumber value="${book.price}" type="currency"/>

Note that because the price is maintained in the database in dollars, the localization is somewhat simplistic, because the formatNumber tag is unaware of exchange rates. The tag formats currencies but does not convert them.

Analogous tags for formatting dates (formatDate) and for parsing numbers and dates (parseNumber, parseDate) are also available. The timeZone tag establishes the time zone (specified with the value attribute) to be used by any nested formatDate tags.

In tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookreceipt.jsp, a “pretend” ship date is created and then formatted with the formatDate tag:

<jsp:useBean id="now" class="java.util.Date" />
<jsp:setProperty name="now" property="time"
     value="${now.time + 432000000}" />
<fmt:message key="ShipDate"/>
 <fmt:formatDate value="${now}" type="date"