Time Zones

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 6.  Formatting Data with Tags


Nearly every Web application must be concerned with time zones. It is very likely that the time zone that your Web server is in is different from the time zone of the user accessing your Web site. JSTL gives you several tags that allow you to set the time zones that date formatting tags deal with.

Setting Time Zones

There are two ways that you can set the time zone in a JSTL-based application. The first is by modifying the web.xml file for the Web site so that it specifies a default time zone. The second is to use the <fmt:timeZone> tag.

Using the <fmt:timeZone> Tag

It is also possible to set the time zone for a specific range of tags. The <fmt:timeZone> will specify the time zone that all tags within its body will use. This can allow you to customize the time zone on a per-user basis. Here is the format of the <fmt:timeZone> tag:

<fmt:timeZone value="timeZone" [var="varName"] body content </fmt:timeZone> 

The <fmt:timeZone> tag accepts the following attribute:

Attribute

Required

Purpose

value

Y

Specifies the time zone that should be used by the tags enclosed in the body of this tag.

The body content of the <fmt:timeZone> tag specifies the tags that will be controlled by this time zone. For example, the following tags would use the U.S. Central time zone:

<fmt:timeZone value="CDT">   <t:formatDate value="${now}" type="both" /> </fmt:timeZone> 

We will show you a program later in this chapter that makes use of multiple time zones.

Specifying the Time Zone in web.xml

If you take no actions to influence the time zone, JSTL will use the time zone that your computer itself is in. If you would like to override your machine's time zone, you can use the web.xml file. The following lines from a web.xml file attempt to set the default time zone to Central time:

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"   "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <context-param>   <param-name>javax.servlet.jsp.jstl.fmt.timeZone</param-name>   <param-value>US/Central</param-value> </context-param> 
Using the <fmt:setTimeZone> Tag

It is possible to load a time zone into an individual scoped variable. This variable will then be passed to the <fmt:timeZone> tag. The <fmt:setTimeZone> tag is used to copy a time zone object into the specified scoped variable. Here is the format of the <fmt:setTimeZone> tag:

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

The attributes accepted by the <fmt:setTimeZone> tag include the following:

Attribute

Required

Purpose

scope

N

Specifies the scope of scoped variable specified by var. Defaults to page.

value

Y

Specifies the time zone that should be used by the tags enclosed in the body of this tag.

var

N

Specifies the scoped variable that is to receive the time zone. If this is not given, a new default time zone will be set.

As you can see, you must specify the time zone using the value attribute. This is done in the same way the time zone is specified using the <fmt:timeZone> tag. For example, specifying the attribute CDT would select the time zone for Central U.S. time. This time zone will then be stored in the variable specified by the var scoped variable. Let's look at a program that makes use of multiple time zones.

Using Time Zones

Listing 6.6 shows a program that will display all of the time zones that Java has access to. The current time will be translated into each of these time zones.

Listing 6.6 Displaying Time Zones (zone.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %> <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> <html>   <head>     <title>Timezones</title>   </head>   <body>     <c-rt:set var="now" value="<%=new java.util.Date()%>" />     <table border="1" cellpadding="0" cellspacing="0"     style="border-collapse: collapse" bordercolor="#111111"     width="63%" >       <tr>         <td width="100%" colspan="2" bgcolor="#0000FF">           <p align="center">             <b>               <font color="#FFFFFF" size="4">Formatting:               <fmt:formatDate value="${now}" type="both"               timeStyle="long" dateStyle="long" />               </font>             </b>           </p>         </td>       </tr>       <c-rt:forEach var="zone"       items="<%=java.util.TimeZone.getAvailableIDs()%>">         <tr>           <td width="51%">             <c:out value="${zone}" />           </td>           <td width="49%">             <fmt:timeZone value="${zone}">               <fmt:formatDate value="${now}" timeZone="${zn}"               type="both" />             </fmt:timeZone>           </td>         </tr>       </c-rt:forEach>     </table>   </body> </html> 

The program in Listing 6.6 goes through every time zone that Java has available. This is done by calling the method java.util.TimeZone.getAvailableIDs(). The following code begins this process:

<c-rt:forEach var="zone"  items="<%=java.util.TimeZone.getAvailableIDs()%>"> 

For each pass through this program, the time zone is displayed:

<c:out value="${zone}" /> 

Finally, the current time is converted to the new time zone. The following code does this by using the <fmt:timeZone> tag:

      <fmt:timeZone value="${zone}">         <fmt:formatDate value="${now}" timeZone="${zn}"         type="both" />       </fmt:timeZone> </c-rt:forEach> 

Our program displays a lengthy list of time zones. You can see the program's output in Figure 6.6.

Figure 6.6. Displaying time zones.

graphics/06fig06.jpg

It is important to make sure that you display the correct time zone whenever possible. If your Web application has anonymous users, then the best you can do is use the local time zone of the server. However, if your system maintains user accounts, you may wish to allow the user to specify a default time zone. In this way, your program could display all times in the time zone for that user. Keep in mind that this is only the time zone that the user uses, though. All internal times stored to your database, or other persistent storage, should be stored in a common time zone. This time zone will usually either be the current time zone of your server or GMT.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch06lev1sec3]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net