Chapter 8. Formatting Actions

   

Topics in This Chapter

  • Formatting and Parsing Numbers

  • Formatting and Parsing Dates and Times

  • Using Time Zones

  • Determining a Formatting Locale

If you want your website to be accessible to as many people as possible, it's crucial to localize text, as discussed in "I18N Actions" on page 248. But it's just as important to localize numbers, dates, and currencies; for example, if you publicized an event on your website and you specified the date as 06/12/2004, Americans would arrive on June 12th, whereas most Europeans would arrive on December 6th. Fortunately, JSTL provides six actions that you can use to format and parse numbers, currencies, percents, and dates:

  • <fmt:formatNumber>

  • <fmt:parseNumber>

  • <fmt:formatDate>

  • <fmt:parseDate>

  • <fmt:timeZone>

  • <fmt:setTimeZone>

The first two actions listed above<fmt:formatNumber> and <fmt:parseNumber>are the inverse of one another. The <fmt:formatNumber> action formats a numeric value, which you can specify as either a string or an instance of java.lang.Number , and outputs that numeric value as a string. The <fmt:parseNumber> action accepts a string representing a formatted number and parses that string into an unformatted numeric value (an instance of java.lang.Number ).

The <fmt:formatDate> and <fmt:parseDate> actions are also the inverse of one another. The <fmt:formatDate> action formats an instance of java.util.Date and outputs a string, whereas <fmt:parseDate> accepts a string representing a formatted date and parses that string into an instance of java.util.Date .

You can use the formatting and parsing actions together to manipulate numeric values; for example, you could:

  1. Parse a formatted number

  2. Perform an arithmetic operation on the numeric value

  3. Reformat the result of that operation as a currency

The last two actions listed above<fmt:timeZone> and <fmt:setTimeZone>let you establish a time zone for the <fmt:formatDate> and <fmt:parseDate> actions. See "Using Time Zones" on page 343 for more information about time zones and the time zone actions.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net