11.7 Formatting Actions

   

The JSTL formatting actions parse and format numbers , currencies, percents, and dates. Four configuration settings support these actions.

Overview of the JSTL Formatting Actions

Table 11.17 lists the JSTL formatting actions.

Table 11.17. Formatting Actions

Action

Description

<fmt:formatNumber>

Formats a numeric value as a number, currency, or percent in a locale-dependent manner.

<fmt:parseNumber>

Parses a string representation of a number, currency, or percent in a locale-dependent manner.

<fmt:formatDate>

Formats a date, time, or both in a locale-dependent manner.

<fmt:parseDate>

Parses the string representation of a date, time, or both in a locale-dependent manner.

<fmt:timeZone>

Sets a time zone used by formatting actions in the body of the <fmt:timeZone> action.

<fmt:setTimeZone>

Sets the FMT_TIME_ZONE configuration setting, which stores a time zone used to parse and format dates.

JSTL Formatting Configuration Settings

The following configuration settings support the JSTL formatting actions:

  • FMT_TIME_ZONE

  • FMT_LOCALE

  • FMT_FALLBACK_LOCALE

  • FMT_LOCALIZATION_CONTEXT

The FMT_TIME_ZONE configuration setting is listed in Table 11.18.

Table 11.18. FMT_TIME_ZONE

Config Constant

FMT_TIME_ZONE

Variable Name

javax.servlet.jsp.jstl.fmt.timeZone

Type

java.lang.String or java.util.TimeZone

Set by

<fmt:setTimeZone>, Deployment Descriptor, Config class

Used by

<fmt:formatDate>, <fmt:parseDate>

The <fmt:formatDate> and <fmt:parseDate> actions require a time zone to format and parse times, respectively. If you do not specify the timeZone attribute for those actions and those actions do not reside in a <fmt:timeZone> action, they will use the time zone stored in the FMT_TIME_ZONE configuration setting. That configuration setting can be set in a number of different ways; one way to set it is with the <fmt:setTimeZone> action. You can also set the FMT_TIME_ZONE configuration setting with a context initialization parameter or in a business component. See "Configuration Settings" on page 230 for more information on how to do that.

The <fmt:formatNumber>, <fmt:parseNumber>, <fmt:formatDate>, and <fmt:parseDate> actions use the locale stored in the FMT_LOCALIZATION_CONTEXT configuration setting if they do not reside in a <fmt:bundle> action and the parseLocale attribute was not specified (for <fmt:parseNumber> and <fmt:parseDate>).

The locale stored in the FMT_LOCALE configuration setting is used by the <fmt:formatNumber>, <fmt:parseNumber>, <fmt:formatDate>, and <fmt:parseDate> actions when those actions do not reside in a <fmt:bundle> action and the localization context in the FMT_LOCALIZATION_CONTEXT configuration setting has not been set or does not have a locale. See "<fmt:bundle>" on page 500 for more information about the <fmt:bundle> action, and see "JSTL Internationalization Configuration Settings" on page 494 for more information about the FMT_LOCALE and FMT_LOCALIZATION_CONTEXT configuration settings.

The <fmt:formatNumber>, <fmt:parseNumber>, <fmt:formatDate>, and <fmt:parseDate> actions use the locale stored in the FMT_FALLBACK_LOCALE configuration setting as a last resort to locate a formatting locale. See "JSTL Internationalization Configuration Settings" on page 494 for more information about the FMT_FALLBACK_LOCALE configuration setting, and see "Formatting Locale Lookup" on page 354 for more information about how formatting actions look up a locale.

JSTL Formatting Actions

<fmt:formatNumber>

Formats a numeric value as a number, currency, or percent in a locale-dependent manner

Syntax: [19]

[19] Items in brackets are optional.

Syntax #1: Without a body

 <fmt:formatNumber value [type] [pattern] [currencyCode]      [currencySymbol] [groupingUsed] [minIntegerDigits]     [maxIntegerDigits] [minFractionDigits] [maxFractionDigits]     [var] [scope]/> 

Syntax #2: With a body that specifies a numeric value to format

 <fmt:formatNumber [type] [pattern] [currencyCode]      [currencySymbol] [groupingUsed] [minIntegerDigits]     [maxIntegerDigits] [minFractionDigits] [maxFractionDigits]     [var] [scope]>  value  </fmt:formatNumber> 

Description:

The <fmt:formatNumber> action formats a numeric value in a locale-dependent manner as a number, currency, or percent. You can specify that numeric value with the value attribute or in the body of the <fmt:formatNumber> action. By default, <fmt:formatNumber> sends its output to the current JspWriter , but if you specify the var attribute, and optionally , the scope attribute, <fmt:formatNumber> stores its output in a scoped variable instead.

Attributes:

Attribute [a]

Type

Description

value

String or Number [b]

The numeric value formatted by <fmt:formatNumber>.

type

String

A string that specifies how the numeric value is formatted. Valid values are number , currency , or percent ; the default is number .

pattern

String

A custom formatting pattern. This attribute takes precedence over the type attribute.

currencyCode

String

A string that indirectly specifies a currency symbol. Currency codes are defined by ISO 4217. This attribute is applied only if you are using JDK 1.4 or later and the type attribute is currency . This attribute takes precedence over the currencySymbol attribute if you are using JDK1.4 or a later version of the JDK.

currencySymbol

String

A string that directly specifies a currency symbol. This attribute is applied only when the type attribute is currency . This attribute takes precedence over the currencyCode attribute if you are using JDK1.3 or an earlier version of the JDK.

groupingUsed

String

A string that specifies whether the grouping separator is used to separate thousands (or ten thousands for some locales); the default is true .

minIntegerDigits

String

The minimum number of integer digits in the formatted output.

maxIntegerDigits

String

The maximum number of integer digits in the formatted output.

minFractionDigits

String

The minimum number of fraction digits in the formatted output.

maxFractionDigits

String

The maximum number of fraction digits in the formatted output.

var

String

The name of a scoped variable. If you specify this attribute, <fmt:formatNumber> stores its output in a scoped variable whose name is specified with this attribute; otherwise , <fmt:formatNumber> sends the formatted output to the current JspWriter .

scope

String

The scope of the scoped variable whose name is specified by the var attribute; default is page scope.

[a] static dynamic

[b] The Number class is from the java.lang package.

Constraints and Error Handling:

  • If you specify the scope attribute, you must also specify the var attribute.

  • You must specify a valid ISO 4217 currency code for the currencyCode attribute.

  • If you specify a string for the value attribute and it cannot be parsed into a valid number, <fmt:formatNumber> will throw a JspException .

  • If you specify something other than number , currency , or percent for the type attribute, <fmt:formatNumber> will throw a JspException .

In a Nutshell:

The <fmt:formatNumber> action formats numbers, currencies, and percents differently for different locales; for example:

 <fmt:setLocale value='en-US'/>  English: <fmt:formatNumber value='1255.23'/> <fmt:setLocale value='de-DE'/> German: <fmt:formatNumber value='1255.23'/> 

In the preceding code fragment, <fmt:formatNumber> formats the string " 1255.23" as 1,255.23 for English and 1.255,23 for German.

Most of the time, the value that you specify for <fmt:formatNumber> will probably be a variable that you read from a data store, such as a database or flat file, so <fmt:formatNumber> lets you specify its value as a scoped variable; for example:

 <%-- Typically, the numericValue scoped variable would be calcu-  lated or created from a value stored in a data store; here, <c:set> is used in the interest of simplicity --%> <c:set var='numericValue' value='234682.155'/> <fmt:formatNumber value='${numericValue}'/> 

Besides specifying a value with the value attribute, you can also specify a value in the body of the <fmt:formatNumber> action. That feature is useful for computed values, such as those produced by a custom action; for example:

 <fmt:formatNumber>     <acme:getSomeNumericValue/> </fmt:formatNumber> 

You can specify whether <fmt:formatNumber> formats its value as a number, currency, or percent with the type attribute, like this:

 <fmt:formatNumber value='1255.23'/>  <fmt:formatNumber value='1255.23' type='currency'/> <fmt:formatNumber value='.2348' type='percent'/> 

For the U.S. English locale, the preceding code fragment will produce the following output: 1,255.23 $1,255.23 23% . If you don't specify the type attribute, as is the case for the first line of code in the preceding code fragment, that attribute defaults to number . Notice that <fmt:formatNumber> formats percents by multiplying the value by 100 and adding a locale-dependent percent symbol.

Most of the <fmt:formatNumber> attributes are used to control exactly how numbers, currencies, and percents are formatted; for example:

 <fmt:formatNumber value='1255.23'         minIntegerDigits='5'       minFractionDigits='5'            groupingUsed='false'/> 

For the U.S. English locale, the preceding code fragment formats the specified value like this: 01255.23000 .

With the currencySymbol attribute, you can directly specify the currency symbol that <fmt:formatNumber> uses to format currencies, like this:

 <fmt:setLocale value='en-US'/>  <fmt:formatNumber value='1255.23' type='currency'          currencySymbol='[$]'/> 

In the preceding code fragment, the <fmt:formatNumber> action formats its value for U.S. currency but uses the currency symbol [$] , so the output is [$]1,255.23 .

You can also indirectly specify a currency symbol with the currencyCode attribute; for example:

 <fmt:setLocale value='fr-FR'/>  <fmt:formatNumber value='1255.23' type='currency'            currencyCode='FRF'/> 

The <fmt:formatNumber> action in the preceding code fragment formats its value for French francs, which is specified with the FRF currency code, so the output is 1 255,23 F .

You can also specify custom formatting patterns for numbers and currencies; for example:

 <fmt:setLocale value='en-US'/>  <fmt:formatNumber value='1255.23' pattern='#,#.000000'/> <fmt:setLocale value='de-DE'/> <fmt:formatNumber value='1255.23' pattern='#,#.000000'/> 

The output of the first <fmt:formatNumber> action in the preceding code fragment is 1,2,5,5.230000 , and the output of the second <fmt:formatNumber> action is 1.2.5.5,230000 . The same formatting pattern is used in each case, but the formatting symbols are locale-dependent; those formatting symbols remain the same, regardless of the <fmt:formatNumber> attributes that you specify.

<fmt:parseNumber>

Parses a string representation of a formatted number, currency, or percent in a locale-dependent manner

Syntax: [20]

[20] Items in brackets are optional.

Syntax #1: Without a body

 <fmt:parseNumber value [type] [pattern] [parseLocale]      [integerOnly] [var] [scope]/> 

Syntax #2: With a body that specifies a number to format

 <fmt:parseNumber [type] [pattern] [parseLocale]      [integerOnly] [var] [scope]>  value  </fmt:parseNumber> 

Description:

The <fmt:parseNumber> action is the inverse of <fmt:formatNumber>: it parses a string representing a formatted number, currency, or percent into a number. You can specify that string with the value attribute or in the body of the <fmt:parseNumber> action. By default, <fmt:parseNumber> sends its output to the current JspWriter , but if you specify the var attribute, <fmt:parseNumber> stores its output in a scoped variable instead.

Attributes:

Attribute [a]

Type

Description

value

String

A string that <fmt:parseNumber> parses as a number, currency, or percent.

type

String

A string that specifies how the string is parsed. Valid values are number , currency , and percent ; the default is number .

pattern

String

A custom parsing pattern. This attribute takes precedence over the type attribute.

parseLocale

String or java.util.Locale

A locale whose default formatting pattern and formatting symbols are used to parse the string specified with the value attribute.

integerOnly

boolean

An attribute that specifies whether only the integer portion of the string specified with the value attribute is parsed. The default value is false .

var

String

The name of a scoped variable. If you specify this attribute, <fmt:parseNumber> stores its output in the scoped variable whose name is specified with this attribute; otherwise, <fmt:parseNumber> sends the parsed output to the current JspWriter .

scope

String

The scope of the scoped variable whose name is specified by the var attribute; default is page scope.

[a] static dynamic

Constraints and Error Handling:

  • If you specify the scope attribute, you must also specify the var attribute.

  • If you specify a value attribute that is null or an empty string and you specify the var attribute, the scoped variable identified by the var attribute will be removed.

  • If you specify the parseLocale attribute as null or as an empty string, that attribute will be ignored.

  • If you specify the pattern attribute as null or as an empty string, that attribute will be ignored.

  • If an exception occurs during parsing, it will be caught by the action and rethrown as a JspException . The message of that JspException will include the value that was to be parsed and the original exception will be provided as the root cause.

  • If you do not specify the parseLocale attribute and <fmt:parseNumber> cannot locate a suitable locale, <fmt:parseNumber> will throw a JspException . That exception will contain the value that was to be parsed.

In a Nutshell:

Sometimes you need to manipulate numeric values that are already formatted as numbers, currencies, or percents. For those types of use cases, you can use <fmt:parseNumber> to parse the original formatted value. You can then adjust the numeric value that <fmt:parseNumber> produces and subsequently use <fmt:formatNumber> to reformat the adjusted numeric value. For example, the following code fragment uses <fmt:parseNumber> in the manner described above:

 <c:set value=',255.23' var='formattedCurrency'/>  <fmt:parseNumber value='${formattedCurrency}'                   type='currency'            parseLocale='en-US'                    var='parsedCurrency'/> <c:set value='${parsedCurrency + 250}' var='updatedAmount'/> <fmt:setLocale value='en-US'/> <fmt:formatNumber value='${updatedAmount}' type='currency'/> 

The preceding code fragment uses the <c:set> action to create a scoped variable that references a formatted currency, but that formatted currency could come from anywhere , perhaps from a Web Service. The <fmt:parseNumber> action parses that formatted currency, using the U.S. English locale and stores the resulting numeric value in a scoped variable named parsedCurrency . Subsequently, the <c:set> action adds 250 to that parsed amount and stores the resulting value in a scoped variable named updatedAmount . Finally, the <fmt:formatNumber> action reformats the updated amount as U.S. currency. The output of the preceding code fragment is $1505.23 .

The <fmt:parseNumber> action can be rather picky about the value it parses ”if its value does not match the attributes that you specify, <fmt:parseNumber> will throw an exception. In the preceding code fragment, if the parsed currency is not formatted correctly, <fmt:parseNumber> throws an exception; for example, if you specify that formatted currency as 1,255.23 instead of $1,255.23 , <fmt:parseNumber> will throw an exception because it cannot parse the specified value as U.S. currency. If <fmt:parseNumber> throws an exception, check the formatting of the value specified for <fmt:parseNumber>.

Like <fmt:formatNumber>, <fmt:parseNumber> has a type attribute that you can set to number , currency , or percent . Unlike <fmt:formatNumber>, <fmt:parseNumber> has a parseLocale attribute that you can use to specify the locale that <fmt:parseNumber> uses to parse its value. The parseLocale attribute was used in the preceding code fragment.

The <fmt:parseNumber> action also has an integerOnly attribute. If you set that attribute to true , <fmt:parseNumber> will only parse the integer portion of its numeric value. You can also specify custom parsing patterns with the pattern attribute, just like you can specify custom formatting patterns for <fmt:formatNumber>.

<fmt:formatDate>

Formats a date, time, or both, in a locale-dependent manner

Syntax: [21]

[21] Items in brackets are optional.

 <fmt:formatDate value [type] [dateStyle] [timeStyle]      [pattern] [timeZone] [var] [scope]/> 

Description:

The <fmt:formatDate> action formats an instance of java.util.Date in a locale-dependent manner. You specify that date with the value attribute. By default, <fmt:formatDate> sends its output to the current JspWriter , but if you specify the var attribute, <fmt:formatDate> stores its output in a scoped variable instead.

Attributes:

Attribute [a]

Type

Description

value

java.util.Date

A date that <fmt:formatDate> formats.

type

String

A string that specifies how the date is formatted. Valid values are date , time , or both ; the default is date .

dateStyle

String

A string that specifies how the date portion of the value attribute will be parsed. Valid values are default , short , medium , long , and full ; the default is default (which is the same as medium ). This attribute is only applied if the type attribute is missing or set to date or both .

timeStyle

String

A string that specifies how the time portion of the value attribute will be formatted. Valid values are default , short , medium , long , and full ; the default is default (which is the same as medium ). This attribute is only applied if the type attribute is set to time or both .

pattern

String

A custom formatting pattern. This attribute takes precedence over the type , dateStyle , and timeStyle attributes.

timeZone

String or java.util.TimeZone

A time zone that <fmt:formatDate> uses to format the value attribute.

var

String

The name of a scoped variable. If you specify this attribute, <fmt:formatDate> stores the formatted output in a scoped variable whose name is specified with this attribute; otherwise, <fmt:formatDate> sends the formatted output to the current JspWriter .

scope

String

The scope of the scoped variable whose name is specified by the var attribute; default is page scope.

[a] static dynamic

Constraints and Error Handling:

  • If you specify the scope attribute, you must also specify the var attribute.

  • If you specify a value attribute that is null and you specify the var attribute, the scoped variable identified by the var attribute will be removed.

  • If you specify the timeZone attribute as null or as an empty string, it will be ignored.

  • If <fmt:formatDate> cannot determine a formatting locale, it formats the ouput with the toString method.

In a Nutshell:

The <fmt:formatDate> action formats an instance of java.util.Date and outputs a string representation of that formatted date. You can create a Date instance with <jsp:useBean>, like this:

 <jsp:useBean id='now' class='java.util.Date'/>  <fmt:formatDate value='${now}'/> 

In the preceding code fragment, <jsp:useBean> creates an instance of java.util.Date , representing the current date and time, which is subsequently formatted by <fmt:formatDate>. You can also create a Date instance with the <fmt:parseDate> action; see "<fmt:parseDate>" on page 522.

The <fmt:formatDate> action will format the same date differently for different locales; for example, if the current date is the 1st of June 1998, the preceding code fragment will output Jun 1, 1998 for U.S English; for France French, the preceding code fragment will output 1 juin 1998 .

All instances of java.util.Date contain date and time information. The <fmt:formatDate> action lets you format the date portion, the time portion, or both the date and time with the <fmt:formatDate> type attribute; for example:

 <jsp:useBean id='now' class='java.util.Date'/>  <fmt:formatDate value='${now}'/> <fmt:formatDate value='${now}' type='time'/> <fmt:formatDate value='${now}' type='both'/> 

If you don't specify the type attribute, it defaults to date . In the preceding code fragment, if the locale is U.S. English and the current time and date is 3:55:41 P.M. on the 15th of May 2002, the first <fmt:formatDate> action will format that date like this: May 15, 2002 . The second <fmt:formatDate> action in the preceding code fragment formats only the time portion of the date and produces this output: 3:55:41 PM . The last <fmt:formatDate> action formats both the date and time, like this: May 15, 2002 3:55:41 PM .

The <fmt:formatDate> dateStyle and timeStyle attributes let you specify a locale-dependent predefined formatting style for dates and times, respectively; for example:

 <jsp:useBean id='now' class='java.util.Date'/>  <fmt:formatDate value='${now}' type='time' timeStyle='short'/> 

The preceding code fragment formats the current time in short format. If the current time is 4:05 in the afternoon, the output of the preceding code fragment for U.S. English will be 4:05 PM . Table 8.4 on page 334 illustrates the various styles for dates and times.

In addition to the predefined date and time styles, you can also specify a custom pattern with the <fmt:formatDate> pattern attribute, like this:

 <jsp:useBean id='now' class='java.util.Date'/>  <fmt:formatDate value='${now}' pattern='MM/dd/yy, hh:mm'/> 

For the U.S. English locale, on the 5th of May 2002 at 4:15 P.M., the preceding code fragment will produce this output: 05/15/02, 04:15 . Table 8.5 on page 337 lists the valid characters that constitute a custom date and time pattern.

You can also specify a time zone with the <fmt:formatDate> timeZone attribute, and <fmt:formatDate> will format its date relative to that time zone; for example:

 <jsp:useBean id='now' class='java.util.Date'/>  <fmt:formatDate value='${now}' type='time' timeStyle='short'/> <fmt:formatDate value='${now}' type='time' timeStyle='short'              timeZone='America/Los_Angeles'/> 

If your server resides in the U.S. Eastern time zone and the current time is 5:00 in the afternoon, the first <fmt:formatDate> action in the preceding code fragment will output 5:00 PM for the U.S. English locale, whereas the second <fmt:formatDate>, which interprets the time relative to the U.S. Pacific time zone, will produce this output: 2:00 PM .

<fmt:parseDate>

Parses the string representation of a formatted date or time, or both, in a locale-dependent manner

Syntax: [22]

[22] Items in brackets are optional.

Syntax #1: Without a body

 <fmt:parseDate value [type] [dateStyle] [timeStyle]      [pattern] [timeZone] [parseLocale] [var] [scope]/> 

Syntax #2: With a body that specifies a date string to parse

 <fmt:parseDate [type] [dateStyle] [timeStyle]      [pattern] [timeZone] [parseLocale] [var] [scope]>  value  </fmt:parseDate> 

Description:

The <fmt:parseDate> action is the inverse of <fmt:formatDate>: it parses a string representing a formatted date, time, or both, into its original, unformatted form. You can specify that string with the value attribute or in the body of the <fmt:parseDate> action. By default, <fmt:parseDate> sends its output to the current JspWriter , but if you specify the var attribute, <fmt:parseDate> stores its output in a scoped variable instead.

Attributes:

Attribute [a]

Type

Description

value

String

A string that <fmt:parseDate> parses as a date, time, or both.

type

String

A string that specifies how the value attribute is parsed. Valid values are date , time , or both ; the default is date .

dateStyle

String

A string that specifies how the date portion of the value attribute will be parsed. Valid values are default , short , medium , long , and full ; the default is default (which is the same as medium ). This attribute is applied only if the type attribute is missing or set to date or both .

timeStyle

String

A string that specifies how the time portion of the value attribute will be parsed. Valid values are default , short , medium , long , and full ; the default is default (which is the same as medium ). This attribute is applied only if the type attribute is set to time or both .

pattern

String

A string that specifies a custom parsing pattern. This attribute takes precedence over the type , dateStyle , and timeStyle attributes.

timeZone

String or java.util.TimeZone

Specifies a time zone that <fmt:parseDate> uses to parse the time portion of the value attribute.

parseLocale

java.util.Locale

Specifies a locale whose formatting styles and formatting symbols for dates and times are used to parse the value attribute

var

String

The name of a scoped variable. If you specify this attribute, <fmt:parseDate> stores its output in a scoped variable whose name is specified with this attribute; otherwise, <fmt:parseDate> sends the parsed result to the current JspWriter .

scope

String

The scope of the scoped variable whose name is specified by the var attribute; default is page scope.

[a] static dynamic

Constraints and Error Handling:

  • If you specify the scope attribute, you must also specify the var attribute.

  • If you specify a value attribute that is null or an empty string and you specify the var attribute, the scoped variable identified by the var attribute will be removed.

  • If you specify the timeZone attribute as null or as an empty string, it will be ignored.

  • If you specify the parseLocale attribute as null or as an empty string, it will be ignored.

  • If an exception occurs during parsing, it will be caught by the action and rethrown as a JspException . The message of that JspException will include the value that was to be parsed, and the original exception will be provided as the root cause.

  • If you do not specify the parseLocale attribute and <fmt:parseDate> cannot locate a suitable locale, a JspException will be thrown. That exception will contain the value that was to be parsed.

In a Nutshell:

The <fmt:parseDate> action parses a string into an instance of java.util.Date . Because <fmt:formatDate> does not accept a string value for the value attribute ”<fmt:formatDate> only formats instances of java.util.Date ”<fmt:parseDate> is commonly used to parse a string to provide a Date instance for <fmt:formatDate>, like this:

 <fmt:parseDate value='06/01/98'                   var='parsedDate'            dateStyle='short'/> <fmt:formatDate value='${parsedDate}'/> 

In the preceding code fragment, <fmt:parseDate> parses the string 06/01/98 and creates a java.util.Date instance that it stores in a scoped variable named parsedDate . Subsequently, <fmt:formatDate> formats that Date instance. The output of the preceding code fragment for the U.S. English locale is Jun 1, 1998 .

The following <fmt:parseDate> attributes let you specify how the string that <fmt:parseDate> parses is interpreted: dateStyle , timeStyle , and parseLocale . You must make sure that those attributes match the string that <fmt:parseDate> parses; for example, in the following code fragment <fmt:parseDate> will throw an exception:

 <%-- This code fragment throws an exception --%>  <fmt:parseDate value='06/01/98'            dateStyle='long'/> 

In the preceding code fragment, the <fmt:parseDate> dateStyle attribute is set to long , but the string that <fmt:parseDate> tries to parse is formatted with the short format for dates. Because of that mismatch, <fmt:parseDate> will throw an exception.

You must also make sure that the parse locale used by <fmt:parseDate> matches the value that <fmt:parseDate> parses; for example, the following code fragment also throws an exception:

 <%-- This code fragment throws an exception also --%>  <fmt:parseDate value='06/20/98'            dateStyle='short'          parseLocale='fr-FR'/> 

In the preceding code fragment, the string that <fmt:parseDate> tries to parse is formatted as MM/dd/yy , where M represents the month, d represents the day, and y represents the year. But because the parseLocale attribute is set to France French and because dates in that locale are formatted as dd/MM/yy , the <fmt:parseDate> action will not be able to parse its string (20 is not a valid month), and therefore that action will throw an exception.

For dates and times that are not formatted according to one of the standard formats ”see Table 8.4 on page 334 for more information about those standard formats ”you can specify a custom pattern with the <fmt:parseDate> pattern attribute, like this:

 <fmt:parseDate value='06/20/98, 10:25'               pattern='MM/dd/yy, hh:mm'/> 

If you specify a custom pattern, as in the preceding code fragment, you must make sure that the pattern matches the string that <fmt:parseDate> tries to parse. For example, in the preceding code fragment, if you change the pattern to MM/dd/yy, hh:mm:ss , the <fmt:parseDate> action will throw an exception.

<fmt:timeZone>

Establishes a time zone for <fmt:formatDate> and <fmt:parseDate> actions in the body of the <fmt:timeZone> action

Syntax:

 <fmt:timeZone value>  body content, presumably with <fmt:formatDate> or <fmt:parseDate> actions  </fmt:timeZone> 

Description:

The <fmt:formatDate> and <fmt:parseDate> actions format and parse dates, respectively, according to a time zone. One way to specify that time zone is with the <fmt:timeZone> action, which establishes a time zone for <fmt:formatDate> and <fmt:parseDate> actions that reside in the body of the <fmt:timeZone> action. You specify that time zone with the <fmt:timeZone> action's value attribute, which can be either a string or an instance of java.util.TimeZone .

Attributes:

Attribute [a]

Type

Description

value

String or java.util.TimeZone

Specifies the time zone used by <fmt:formatDate> and <fmt:parseDate> actions in the body of the <fmt:timeZone> action. If you specify the time zone as a string, you must use a time zone ID supported by the Java platform, such as America/Denver .

[a] static dynamic

Constraints and Error Handling:

  • If you specify the value attribute as null or an empty string, <fmt:timeZone> uses the GMT time zone.

In a Nutshell:

You can use the <fmt:timeZone> action to specify a time zone for <fmt:parseDate> and <fmt:formatDate> actions, like this:

 <jsp:useBean id='now' class='java.util.Date'/>  <fmt:timeZone value='America/New_York'>    <fmt:formatDate value='${now}'                     type='time'                timeStyle='full'/> </fmt:timeZone> 

In the preceding code fragment, if the current time is 10:30 A.M. MDT, the <fmt:formatDate> action will produce this output: 12:30:00 PM EDT .

You can override the time zone specified with the <fmt:timeZone> action with the timeZone attribute for <fmt:formatDate> and <fmt:parseDate>; for example:

 <jsp:useBean id='now' class='java.util.Date'/>  <fmt:timeZone value='America/New_York'>    <fmt:formatDate value='${now}'                     type='both'                timeStyle='full'/>    <fmt:formatDate value='${now}'                     type='both'                 timeZone='America/Denver'                timeStyle='full'/> </fmt:timeZone> 

In the preceding code fragment, the first <fmt:formatDate> action formats the current date and time for U.S. Eastern Time, whereas the second <fmt:formatDate> formats the current date and time for Mountain Daylight Time.

<fmt:setTimeZone>

Establishes a time zone for <fmt:formatDate> and <fmt:parseDate> actions

Syntax: [23]

[23] Items in brackets are optional.

 <fmt:setTimeZone value [var] [scope]/> 

Description:

The <fmt:formatDate> and <fmt:parseDate> actions format and parse dates, respectively, according to a time zone. One way to specify that time zone is with the <fmt:setTimeZone> action, which stores a time zone in the FMT_TIME_ZONE configuration setting or, if you specify the var attribute, a scoped variable. You specify that time zone with the value attribute, which can be either a string or an instance of java.util.TimeZone .

Attributes:

Attribute [a]

Type

Description

value

String or java.util.TimeZone

A time zone used by <fmt:formatDate> and <fmt:parseDate> actions. If you specify the time zone as a string, you must use a time zone ID supported by the Java platform, such as America/Denver .

Var

String

The name of a scoped variable. If you specify this attribute, <fmt:setTimeZone> stores the specified time zone in a scoped variable whose name is specified with this attribute; otherwise, <fmt:setTimeZone> stores the time zone in the FMT_TIME_ZONE configuration setting.

scope

String

The scope of the scoped variable named by the var attribute, or if that attribute is not specified, the scope of the FMT_TIME_ZONE configuration setting; default is page scope

[a] static dynamic

Constraints and Error Handling:

  • If you specify the value attribute as null or an empty string, <fmt:timeZone> uses the GMT time zone.

In a Nutshell:

The <fmt:setTimeZone> action stores a time zone in the FMT_TIME_ZONE configuration setting or in a scoped variable. This action is useful if you need to set a time zone for <fmt:formatDate> and <fmt:parseDate> actions that span one or more JSP pages. There are a number of ways that you can use <fmt:setTimeZone>; here are some examples:

 <fmt:setTimeZone value='America/New_York'/>  <fmt:setTimeZone value='America/New_York' scope='request'/> <fmt:setTimeZone value='America/New_York' var='timeZone'/> <fmt:setTimeZone value='America/New_York' var='timeZone'                  scope='session'/> 

In the preceding code fragment, the first line of code sets the America/New_York Time time zone for the current JSP page. It does that by storing the time zone in a configuration variable named FMT_TIME_ZONE in page scope, which overrides the configuration setting of the same name in that scope. If the FMT_TIME_ZONE configuration setting was previously set for a scope other than page scope, that setting will be restored when the current JSP page is no longer active. See "Configuration Settings" on page 230 for more information about JSTL configuration settings.

The second line of code in the preceding code fragment stores the America/New_York time zone in the FMT_TIME_ZONE configuration variable for request scope, making that time zone the default for the current HTTP request. If the FMT_TIME_ZONE configuration setting was previously set for session or application scope, that setting will be restored when the current HTTP request is finished.

The last two lines of code in the preceding code fragment store the America/New_York Time time zone in a scoped variable. The third line of code stores that scoped variable in page scope, and the last line of code stores that scoped variable in session scope. You can access that scoped variable like this:

 <fmt:formatDate value='${now}'                   type='both'  timeZone='${timeZone}'  timeStyle='full'/> 
   


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