Core JSTL[c] Mastering the JSP Standard Tag Library

   

The <fmt:formatDate> and <fmt:parseDate> actions use a time zone to format and parse dates, respectively. Both of those actions have a timeZone attribute that lets you explicitly specify a time zone; for example:

<fmt:parseDate value='6/20/1957 14:00' timeZone='America/Los_Angeles' var='parsedDate' pattern='MM/dd/yyyy kk:mm'/> Date in Eastern Time Zone: <fmt:formatDate value='${parsedDate}' timeZone='America/New_York' type='both' timeStyle='full'/>

In the preceding code fragment, <fmt:parseDate> parses a string and stores the resulting date in a scoped variable named parsedDate . [12] The timeZone attribute tells <fmt:parseDate> that the date it parses is relative to the America/Los_Angeles time zone. Subsequently, the <fmt:formatDate> action formats the date stored in the parsedDate scoped variable. The timeZone attribute tells <fmt:formatDate> that it should interpret the date it formats relative to the America/New_York time zone. Here's the output for the preceding code fragment:

[12] That date is an instance of java.util.Date ; see "Formatting and Parsing Dates and Times" on page 333 for more information about <fmt:parseDate>.

Date in Eastern Time Zone: Jun 20, 1957 5:00:00 PM EDT

Notice that the <fmt:formatDate> action in the preceding code fragment adjusts the time from the America/Los_Angeles time zone to the America/New_York time zone, taking into account Standard Daylight Time.

If you don't specify a timeZone attribute for <fmt:formatDate> or <fmt:parseDate>, those actions search for a time zone like this:

  1. If the <fmt:formatDate> or <fmt:parseDate> actions are nested in a <fmt:timeZone> action, they use the time zone specified by that <fmt:timeZone> action; if those actions are not nested in a <fmt:timeZone> action, go to step 2.

  2. If the FMT_TIME_ZONE configuration setting has been set, <fmt:formatDate> and <fmt:parseDate> will use the time zone specified by that configuration setting; if the FMT_TIME_ZONE configuration setting has not been set, go to step 3.

  3. The <fmt:formatDate> and <fmt:parseDate> actions use the JSP container's time zone.

The <fmt:timeZone> action, mentioned in step 1 listed above, has a simple syntax: [13]

[13] See "<fmt:timeZone>" on page 525 for a complete description of <fmt:timeZone>.

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

Instead of using the timeZone attributes for <fmt:formatDate> and <fmt:parseDate>, you can use the <fmt:timeZone> action to specify a time zone for those actions; for example, the code fragment listed at the beginning of this section could be rewritten like this:

<fmt:timeZone value='America/Los_Angeles'> <fmt:parseDate value='6/20/1957 14:00' var='parsedDate' pattern='MM/dd/yyyy kk:mm'/> </fmt:timeZone> Date in Eastern Time Zone: <fmt:timeZone value='America/New_York'> <fmt:formatDate value='${parsedDate}' type='both' timeStyle='full'/> </fmt:timeZone>

The preceding code fragment is functionally identical to the code fragment listed at the beginning of this section. The <fmt:timeZone> action is preferable to the timeZone attribute for <fmt:parseDate> and <fmt:formatDate> when a number of <fmt:parseDate> and <fmt:formatDate> actions share a time zone.

If you don't specify the timeZone attribute for <fmt:parseDate> and <fmt:formatDate> actions and if those actions are not nested in a <fmt:timeZone> action, they will use the time zone stored in the FMT_TIME_ZONE configuration setting. You can set that configuration setting in a number of ways; [14] for example, you can set the FMT_TIME_ZONE configuration setting in a deployment descriptor, like this:

[14] See "Configuration Settings" on page 230 for more information about configuration settings.

<web-app> ... <context-param> <param-name> javax.servlet.jsp.jstl.fmt.timeZone </param-name> <param-value>America/New_York</param-value> </context-param> ... <web-app>

The preceding code fragment, from WEB-INF/web.xml , specifies a time zone of America/New_York for the FMT_TIME_ZONE configuration setting. Notice that the preceding code fragment specifies a parameter name of javax.servlet.jsp.jstl.fmt.timeZone , which is the name of the FMT_TIME_ZONE configuration setting. [15] If you set the FMT_TIME_ZONE configuration setting to America/New_York , as in the preceding code fragment, the following code fragment is equivalent to the code fragment listed at the beginning of this section:

[15] See "JSTL Formatting Configuration Settings" on page 510 for more information about the FMT_TIME_ZONE configuration setting.

<fmt:parseDate value='6/20/1957 14:00' timeZone='America/Los_Angeles' var='parsedDate' pattern='MM/dd/yyyy kk:mm'/> Date in Eastern Time Zone: <fmt:formatDate value='${parsedDate}' type='both' timeStyle='full'/>

In the preceding code fragment, the <fmt:formatDate> action will use the time zone specified with the FMT_TIME_ZONE configuration setting. You can also set the FMT_TIME_ZONE configuration setting with the <fmt:setTimeZone> action. That action has the following syntax: [16]

[16] Items in brackets are optional. See "<fmt:setTimeZone>" on page 527 for a complete description of <fmt:setTimeZone> syntax.

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

You could use the <fmt:setTimeZone> action to rewrite the code fragment listed at the beginning of this section, like this:

< fmt:setTimeZone value='America/Los_Angeles'/> <fmt:parseDate value='6/20/1957 14:00' var='parsedDate' pattern='MM/dd/yyyy kk:mm'/> Date in Eastern Time Zone: < fmt:setTimeZone value='America/New_York'/> <fmt:formatDate value='${parsedDate}' type='both' timeStyle='full'/>

In the preceding code fragment, the <fmt:setTimeZone> actions store a time zone in the FMT_TIME_ZONE configuration setting. Because the timeZone attribute is not set for the <fmt:parseDate> and <fmt:formatDate> actions in the preceding code fragment and because those actions are not nested in a <fmt:timeZone> action, they will use the time zone stored in the FMT_TIME_ZONE configuration setting.

You can also use the <fmt:setTimeZone> var attribute to store a time zone in a scoped variable instead of the FMT_TIME_ZONE configuration setting, like this:

<fmt:setTimeZone value='America/Los_Angeles' var='parseTimeZone'/> <fmt:parseDate value='6/20/1957 14:00' var='parsedDate' timeZone='${ parseTimeZone }' pattern='MM/dd/yyyy kk:mm'/> Date in Eastern Time Zone: <fmt:setTimeZone value='America/New_York' var='formatTimeZone'/> <fmt:formatDate value='${parsedDate}' timeZone='${ formatTimeZone }' type='both' timeStyle='full'/>

In the preceding code fragmentwhich is functionally identical to the code fragment listed at the beginning of this sectionthe <fmt:setTimeZone> action is used to store the America/Los_Angeles time zone in a scoped variable named parseTimeZone and the America/New_York time zone in a scoped variable named formatTimeZone . Those scoped variables are used by the <fmt:parseDate> and <fmt:formatDate> actions, respectively. When you need to temporarily set a time zone that spans multiple pagesfor example, if you need to set a time zone for a particular HTTP requestthe <fmt:setTimeZone> action with the var attribute specified is preferable to the timeZone attribute for <fmt:parseDate> and <fmt:formatDate> and the FMT_TIME_ZONE configuration setting.

Finally, if you do not specify the timeZone attribute for <fmt:parseDate> and <fmt:formatDate> actions, those actions are not nested in a <fmt:timeZone> action, and if the FMT_TIME_ZONE configuration setting has not been set, those actions will use your JSP container's default time zone.

You must always specify time zones with time zone Standard IDs, which are of the form Continent/City, such as America/New_York; Ocean/City, such as Pacific/Honolulu; or Continent/Region/City, such as America/Indiana/Knox. Table 8.7 lists some examples of time zone Standard IDs.

Many time zones have abbreviations; for example America/Los_Angeles is often abbreviated as PST or PDT, for Pacific Standard Time and Pacific Daylight Time, respectively. But you must not use time zone abbreviations when you specify time zones in your JSP code, because abbreviations are highly ambiguous; for example, IST can mean either Irish Summer Time or India Standard Time.

For a complete list of time zone Standard IDs, see the following URL:

http://www.timezoneconverter.com/cgi-bin/zonehelp.tzc

Table 8.7. Time Zone Standard ID Examples

Standard ID

Comment

America/Adak

Aleutian Islands

America/Chicago

Central Time

America/Dawson

Pacific Time - North Yukon

America/Denver

Mountain Time

America/Detroit

Eastern Time, Michigan (most locations)

America/Los_Angeles

Pacific Time

America/Louisville

Eastern Time, Kentucky

America/New_York

Eastern Time

America/Noronha

Atlantic Islands

America/Yakutat

Alaska Time, Alaska panhandle neck

Pacific/Honolulu

Hawaii

In addition to providing a list of time zones, the preceding URL provides a link to a time zone converter. That converter lets you convert the current date and time from one time zone to another. Figure 8-6 shows a time zone converter that uses the JSTL formatting actions.

Figure 8-6. A JSTL Time Zone Converter

The first time the JSP page shown in Figure 8-6 is accessed, it selects the option in the HTML select element that represents the JSP container's time zone and displays the current date and time for that time zone. The top picture in Figure 8-6 shows the JSP page when it is first accessed.

After you select a time zone and activate the submit button, the JSP page shown in Figure 8-6 displays the current date and time for the JSP container's time zone and for the selected time zone. The bottom picture in Figure 8-6 shows the JSP page after the America/Montreal time zone has been selected.

The JSP page shown in Figure 8-6 is listed in Listing 8.5.

The preceding JSP page imports the TimeZone , Date , and Locale classes from the java.util package and creates a scoped variable named now that references a Date object representing the current date and time. Subsequently, the JSP page uses the <c_rt:set> action to store the default time zone, the name of the default time zone localized in U.S. English, and all of the available time zone Standard IDs in scoped variables, which are used later in the page. The preceding JSP page then creates its form. If no time zone was previously selected, the JSP page selects the option for the HTML select element that represents the JSP container's time zone; otherwise , the JSP page selects the previously selected time zone.

Listing 8.5 A TimeZone Converter

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>TimeZone Converter</title> </head> <body> <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %> <%@ taglib uri='http://java.sun.com/jstl/fmt' prefix='fmt'%> <%@ taglib uri='http://java.sun.com/jstl/core_rt' prefix='c_rt' %> <%-- These classes are needed below --%> <%@ page import='java.util.TimeZone' %> <%@ page import='java.util.Date' %> <%@ page import='java.util.Locale' %> <%@ page import='javax.servlet.jsp.jstl.core.Config' %> <%-- Set the locale to U.S. English --%> <fmt:setLocale value='en-US'/> <%-- Use this bean below --%> <jsp:useBean id='now' class='java.util.Date'/> <%-- Store the default time zone in a scoped variable --%> <c_rt:set value='<%= TimeZone.getDefault() %>' var='defaultTimeZone'/> <%-- Store the name of the default time zone in a scoped variable --%> <c_rt:set value='<%= TimeZone.getDefault(). getDisplayName(Locale.US) %>' var='defaultTimeZoneName'/> <%-- Store all available time zone Standard IDs in a scoped variable --%> <c_rt:set value='<%= TimeZone.getAvailableIDs() %>' var='availableTimeZones'/> <%-- Because this form does not specify an action, this JSP page will be reloaded when the form's submit button is activated --%> <form> <%-- Create the HTML select element for selecting a time zone --%> <select name='timeZone'> <%-- For each available time zone --%> <c:forEach var='item' items='${availableTimeZones}'> <option <c:choose> <%-- If no time zone was previously selected --%> <c:when test='${empty param.timeZone}'> <%-- Select this option if it's equal to the default time zone --%> <c_rt:if test='<%= TimeZone.getDefault(). equals(TimeZone.getTimeZone((String) Config.find(pageContext, "item")))%>'> selected </c_rt:if> </c:when> <%-- Select this option if it equals the last time zone selected --%> <c:when test='${param.timeZone == item}'> selected </c:when> </c:choose> ><c:out value='${item}'/></option> </c:forEach> </select> <input type='submit'/> </form> <%-- Display the current date and time in the JSP container's time zone --%> Current Date and Time in Your Server's TimeZone<i> (<c:out value='${defaultTimeZoneName}'/>)</i>: <fmt:formatDate value='${now}' type='both' timeZone='${defaultTimeZone}'/> <br> <c:choose> <%-- If a time zone was previously selected, show the date and time for that time zone --%> <c:when test='${not empty param.timeZone}'> Current Date and Time in the <i><c:out value=' ${param.timeZone} TimeZone: '/></i> <fmt:timeZone value='${param.timeZone}'> <fmt:formatDate value='${now}' type='both'/> </fmt:timeZone> </c:when> </c:choose> </body> </html>

After the form has been created, the preceding JSP page displays the current date and time for the JSP container's time zone. If a time zone was previously selected, the JSP page also displays the current date and time for that time zone.

For the sake of illustration, the preceding JSP page formats the current date and time in two different ways. The date and time for the JSP container's time zone are formatted by specification of the timeZone attribute for the <fmt:formatDate> action. The date and time for the previously selected time zone are formatted by a <fmt:formatDate> action that is nested in a <fmt:timeZone> action. Specifying time zones in two different ways reinforces that there is more than one way to specify a time zone for <fmt:formatDate> and <fmt:parseDate> actions; see "Using Time Zones" on page 343 for more information about other ways to specify time zones.

   

Категории