| This Chapter | |
| - | Chapter 7: JSTL |
| - | Introducing JSTL |
| - | General-Purpose Actions |
| - | Conditional Actions |
| - | Iterator Actions |
| - | Formatting Actions |
| - | Functions |
| - | Summary |
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.
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.
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.
| 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
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
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
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}"/>