8.3 Using Time ZonesThe <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:
In the preceding code fragment, <fmt:parseDate> parses a string and stores the resulting date in a scoped variable named parsedDate.Formatting and Parsing Dates and Times" on page 333 for more information about <fmt:parseDate>.
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:
The <fmt:timeZone>" on page 525 for a complete description of <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:
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 Configuration Settings" on page 230 for more information about configuration settings.
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.JSTL Formatting Configuration Settings" on page 510 for more information about the FMT_TIME_ZONE configuration setting.
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:setTimeZone>" on page 527 for a complete description of <fmt:setTimeZone> syntax.
You could use the <fmt:setTimeZone> action to rewrite the code fragment listed at the beginning of this section, like this:
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:
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
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
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. |