Home

This Chapter
-Chapter 7: JSTL
-Introducing JSTL
-General-Purpose Actions
-Conditional Actions
-Iterator Actions
-Formatting Actions
-Functions
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Formatting Actions

JSTL provides tags for formatting and parsing numbers and dates. The tags are formatNumber, formatDate, timeZone, setTimeZone, parseNumber, and parseDate. These tags are discussed in the sections to follow.

The formatNumber Tag

You use formatNumber to format numbers. This tag gives you the flexibility of using its various attributes to get a format that suits your need. The syntax of formatNumber has two forms. The first is used without a body content:

<fmt:formatNumber value="numericValue"
  [type="{number|currency|percent}"]
  [pattern="customPattern"]
  [currencyCode="currencyCode"]
  [currencySymbol="currencySymbol"]
  [groupingUsed="{true|false}"]
  [maxIntegerDigits="maxIntegerDigits"]
  [minIntegerDigits="minIntegerDigits"]
  [maxFractionDigits="maxFractionDigits"]
  [minFractionDigits="minFractionDigits"]
  [var="varName"]
  [scope="{page|request|session|application}"]/>

The second form is used with a body content:

<fmt:formatNumber [type="{number|currency|percent}"]
  [pattern="customPattern"]
  [currencyCode="currencyCode"]
  [currencySymbol="currencySymbol"]
  [groupingUsed="{true|false}"]
  [maxIntegerDigits="maxIntegerDigits"]
  [minIntegerDigits="minIntegerDigits"]
  [maxFractionDigits="maxFractionDigits"]
  [minFractionDigits="minFractionDigits"]
  [var="varName"]
  [scope="{page|request|session|application}"]>
  numeric value to be formatted
</fmt:formatNumber>

The body content is JSP. The list of the formatNumber tag’s attributes is given in Table 7.8.

Attribute Type Description
value+ String or Number Numeric value to be formatted.
type+ String Indicates whether the value is to be formatted as number, currency, or percentage. The value of this attribute is one of the following: number, currency, percent.
pattern+ String Custom formatting pattern.
currencyCode+ String ISO 4217 code. See Table 7.9.
currencySymbol+ String Currency symbol.
groupingUsed+ Boolean Indicates whether the output will contain grouping separators.
maxIntegerDigits+ int The maximum number of digits in the integer portion of the output.
minIntegerDigits+ int The minimum number of digits in the integer portion of the output.
maxFractionDigits+ int The maximum number of digits in the fractional portion of the output.
minFractionDigits+ int The minimum number of digits in the fractional portion of the output.
var String The name of the scoped variable to store the output as a String.
scope String The scope of var. If the scope attribute is present, the var attribute must be specified.

Table 7.8: The formatNumber tag’s attributes

One of the uses of formatNumber is to format numbers as currencies. For this, you can use the currencyCode attribute to specify an ISO 4217 currency code. Some of the codes are given in Table 7.9.

Currency ISO 4217 Code Major Unit Name Minor Unit Name
Canadian Dollar CAD dollar cent
Chinese Yuan CNY yuan renminbi jiao
Euro EUR euro euro-cent
Japanese Yen JPY yen sen
Sterling GBP pound pence
US Dollar USD dollar cent

Table 7.9: ISO 4217 Currency Codes

The examples of how to use formatNumber are given in Table 7.10.

Action Result
<fmt:formatNumber value="12" type="number"/> 12
<fmt:formatNumber value="12" type="number" minIntegerDigits="3"/> 012
<fmt:formatNumber value="12" type="number" minFractionDigits="2"/> 12.00
<fmt:formatNumber value="123456.78" pattern=".000"/> 123456.780
<fmt:formatNumber value="123456.78" pattern="#,#00.0#"/> 123,456.78
<fmt:formatNumber value="12" type="currency"/> $12.00
<fmt:formatNumber value="12" type="currency" currencyCode="GBP"/> GBP 12.00
<fmt:formatNumber value="0.12" type="percent"/> 12%
<fmt:formatNumber value="0.125" type="percent" minFractionDigits="2"/> 12.50%

Table 7.10: Using the formatNumber tag

Note that when formatting currencies, if the currencyCode attribute is not specified, the client browser's locale is used.

The formatDate Tag

You use the formatDate tag to format dates. The syntax is as follows:

<fmt:formatDate value="date"
  [type="{time|date|both}"]
  [dateStyle="{default|short|medium|long|full}"]
  [timeStyle="{default|short|medium|long|full}"]
  [pattern="customPattern"]
  [timeZone="timeZone"]
  [var="varName"]
  [scope="{page|request|session|application}"]/>

The body content is JSP. The list of the formatDate tag’s attributes is given in Table 7.11.

Table 7.11: The formatDate tag’s attributes

For possible values of the timeZone attribute, see the section, “The timeZone Tag”.

The following code uses the formatDate tag to format the java.util.Date object referenced by the scoped variable now.

Default: <fmt:formatDate value="${now}"/>
Short: <fmt:formatDate value="${now}" dateStyle="short"/>
Medium: <fmt:formatDate value="${now}" dateStyle="medium"/>
Long: <fmt:formatDate value="${now}" dateStyle="long"/>
Full: <fmt:formatDate value="${now}" dateStyle="full"/>

The following formatDate tags are used to format times.

Default: <fmt:formatDate type="time" value="${now}"/>
Short: <fmt:formatDate type="time" value="${now}"
  timeStyle="short"/>
Medium: <fmt:formatDate type="time" value="${now}"
  timeStyle="medium"/>
Long: <fmt:formatDate type="time" value="${now}" timeStyle="long"/>
Full: <fmt:formatDate type="time" value="${now}" timeStyle="full"/>

The following formatDate tags format both dates and times.

Default: <fmt:formatDate type="both" value="${now}"/>
Short date short time: <fmt:formatDate type="both"
  value="${now}" dateStyle="short" timeStyle="short"/>
Long date long time format: <fmt:formatDate type="both"
  value="${now}" dateStyle="long" timeStyle="long"/>

The following formatDate tags are used to format times with time zones.

Time zone CT: <fmt:formatDate type="time" value="${now}"
  timeZone="CT"/><br/>
Time zone HST: <fmt:formatDate type="time" value="${now}"
  timeZone="HST"/><br/>

The following formatDate tags are used to format dates and times using custom patterns.

<fmt:formatDate type="both" value="${now}" pattern="dd.MM.yy"/>
<fmt:formatDate type="both" value="${now}" pattern="dd.MM.yyyy"/>

The timeZone Tag

The timeZone tag is used to specify the time zone in which time information is to be formatted or parsed in its body content. The syntax is as follows:

<fmt:timeZone value="timeZone">
  body content
</fmt:timeZone>

The body content is JSP. The attribute value can be passed a dynamic value of type String or java.util.TimeZone. The values for US and Canada time zones are given in Table 7.12.

If the value attribute is null or empty, the GMT time zone is used.

<fmt:timeZone value="GMT+1:00">
  <fmt:formatDate value="${now}" type="both" dateStyle="full"
  timeStyle="full"/>
</fmt:timeZone>
<fmt:timeZone value="HST">
  <fmt:formatDate value="${now}" type="both" dateStyle="full"
  timeStyle="full"/>
</fmt:timeZone>
<fmt:timeZone value="CST">
  <fmt:formatDate value="${now}" type="both" dateStyle="full"
  timeStyle="full"/>
</fmt:timeZone>
Attribute Type Description
value+ java.util.Date Date and/or time to be formatted
type+ String Indicates whether the time, the date, or both the time and the date components are to be formatted
dateStyle+ String Predefined formatting style for dates that follows the semantics defined in java.text.DateFormat.
timeStyle+ String Predefined formatting style for times that follows the semantics defined in java.text.DateFormat.
pattern+ String The custom pattern for formatting
timezone+ String or java.util.TimeZone The time in which to represent the time
var String The name of the scoped variable to store the result as a string
scopeString Scope of var.
Abbreviation Full Name Time Zone
NST Newfoundland Standard Time UTC-3:30 hours
NDT Newfoundland Daylight Time UTC-2:30 hours
AST Atlantic Standard Time UTC-4 hours
ADT Atlantic Daylight Time UTC-3 hours
EST Eastern Standard Time UTC-5 hours
EDT Eastern Daylight Saving Time UTC-4 hours
ET Eastern Time, as EST or EDT *
CST Central Standard Time UTC-6 hours
CDT Central Daylight Saving Time UTC-5 hours
CT Central Time, as either CST or CDT *
MST Mountain Standard Time UTC-7 hours
MDT Mountain Daylight Saving Time UTC-6 hours
MT Mountain Time, as either MST or MDT *
PST Pacific Standard Time UTC-8 hours
PDT Pacific Daylight Saving Time UTC-7 hours
PT Pacific Time, as either PST or PDT *
AKST Alaska Standard Time UTC-9 hours
AKDT Alaska Standard Daylight Saving Time UTC-8 hours
HST Hawaiian Standard Time UTC-10 hours

Table 7.12: US and Canada Time Zones

The setTimeZone Tag

You use the setTimeZone tag to store the specified time zone in a scoped variable or the time configuration variable. The syntax of setTimeZone is as follows:

<fmt:setTimeZone value="timeZone" [var="varName"]
  [scope="{page|request|session|application}"]/>

Table 7.13 presents the setTimeZone tag’s attributes.

Attribute Type Description
value+ String or java.util.TimeZone The time zone
var String The name of the scoped variable to hold the time zone of type java.util.TimeZone
scope String The scope of var or the time zone configuration variable

Table 7.13: The setTimeZone tag’s attributes

The parseNumber Tag

You use parseNumber to parse a string representation of a number, a currency, or a percentage in a locale-sensitive format into a number. The syntax has two forms. The first form is used without a body content:

<fmt:parseNumber value="numericValue"
  [type="{number|currency|percent}"]
  [pattern="customPattern"]
  [parseLocale="parseLocale"]
  [integerOnly="{true|false}"]
  [var="varName"]
  [scope="{page|request|session|application}"]/>

The second form is used with a body content:

<fmt:parseNumber [type="{number|currency|percent}"]
  [pattern="customPattern"]
  [parseLocale="parseLocale"]
  [integerOnly="{true|false}"]
  [var="varName"]
  [scope="{page|request|session|application}"]>
  numeric value to be parsed
  </fmt:parseNumber>

The content body is JSP. The parseNumber tag’s attributes are given in Table 7.14.

As an example, the following parseNumber tag parses the value referenced by the scoped variable quantity and stores the result in the formattedNumber scoped variable.

<fmt:parseNumber var="formattedNumber" type="number"
  value="${quantity}"/>
Attribute Type Description
value+ String String to be parsed
type+ String Indicates whether the string to be parsed is to be parsed as a number, currency, or percentage
pattern+ String Custom formatting pattern that determines how the string in the value attribute is to be parsed
parseLocale+ String or java.util.Locale Locale whose default formatting pattern is to be used during the parse operation, or to which the pattern specified via the pattern attribute is applied
integerOnly+ Boolean Indicates whether only the integer portion of the given value should be parsed
var String The name of the scoped variable to hold the result
scope String The scope of var

Table 7.14: The parseNumber tag’s attributes

The parseDate Tag

parseDate parses the string representation of a date and time in locale-sensitive format. The syntax has two forms. The first form is used without a body content:

<fmt:parseDate value="dateString"
  [type="{time|date|both}"]
  [dateStyle="{default|short|medium|long|full}"]
  [timeStyle="{default|short|medium|long|full}"]
  [pattern="customPattern"]
  [timeZone="timeZone"]
  [parseLocale="parseLocale"]
  [var="varName"]
  [scope="{page|request|session|application}"]/>

The second form is used with a body content:

<fmt:parseDate [type="{time|date|both}"]
  [dateStyle="{default|short|medium|long|full}"]
  [timeStyle="{default|short|medium|long|full}"]
  [pattern="customPattern"]
  [timeZone="timeZone"]
  [parseLocale="parseLocale"]
  [var="varName"]
  [scope="{page|request|session|application}"]>
  date value to be parsed
</fmt:parseDate>

The body content is JSP. Table 7.15 lists the parseDate tag’s attributes.

Attribute Type Description
value+ String String to be parsed
type+ String Indicates whether the string to be parsed contains a date, a time, or both
dateStyle+ String The formatting style of the date
timeStyle+ String
pattern+ String Custom formatting pattern that determines how the string is to be parsed
timeZone+ String or java.util.TimeZone Time zone in which to interpret any time information in the date string
parseLocale+ String or java.util.Locale Locale whose default formatting pattern is to be used during the parse operation, or to which the pattern specified via the pattern attribute is applied
var String The name of the scoped variable to hold the result
scope String The scope of var

Table 7.15: The parseDate tag’s attributes

As an example, the following parseDate tag parses a date referenced by the scoped variable myDate and stores the resulting java.util.Date in a page-scoped variable formattedDate.

<c:set var="myDate" value="12/12/2005"/>
<fmt:parseDate var="formattedDate" type="date"
  dateStyle="short" value="${myDate}"/>

Previous
Next