JSTL timeZone & setTimeZone fmt Tag Example <fmt:timeZone> <fmt:setTimeZone>

<fmt:timeZone> tag is used to specify the time zone. It’s scope is limited to its body, it parses the actions that are nested into its body.

The <fmt:timeZone> tag is used to specify the time zone information for any date formatting in its body. This tag is different from <fmt:setTimeZone> in that, <fmt:setTimeZone> is used to set the default time zone. However, the <fmt:timeZone> tag sets the time zone for the tags within its body. Rest of the JSP page uses the time zone set by the <fmt:setTimeZone> or the default time zone.

Syntax-
<fmt:timeZone value=”<string>”/>

Attributes of <fmt:timeZone> tag:

value : This is a required attribute that specifies the name of a time zone/time zone IDs supported by java or an instance of the java.util.TimeZone.

The following JSP code displays the current date using the default time zone and displays current date using the GMT-8 time zone. The GMT-8 time zone is set using the <fmt:timeZone> tag. Notice that we are displaying the date within the body of the <fmt:timeZone> tag.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;fmt:timeZone&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;fmt:timeZone&gt; Demo</h1>
    <c:set var="today" value="<%=new java.util.Date()%>" />
    <c:set var="timeZone" value="GMT-8"/>
    Date in the current time zone:
    <strong><fmt:formatDate value="${today}" type="both" /></strong>
    <br/>
    Date in the GMT-8 time zone (nested in &lt;fmt:timeZone&gt; tag):
    <fmt:timeZone value="${timeZone}">
    <strong>
          <fmt:formatDate value="${today}" timeZone="${timeZone}" type="both" />
        </strong>
    </fmt:timeZone>
  </body>
</html>

Output–

JSTL fmt Tag <fmt:setTimeZone> Example-

The <fmt:setTimeZone> is used to set the required time zone value. We can also copy the time zone object into the scoped variable for later use.

In JSP <fmt:setTimeZone> tag of JSTL fmt tag library is used to set a specified time zone or default time zone. The specified time zone may be either a specific name of time zone, supported by java or an instance of the TimeZone class of Java’s utility package and the name or an instance can be passed into the required attribute named “value” of this tag.

Attributes of <fmt:setTimeZone> tag
value : This is a required attribute that specifies the name of a time zone/time zone IDs supported by java or an instance of the java.util.TimeZone.
var : This is an optional attribute that defines the name of the scoped variable kept the time zone of java.util.TimeZone type.
scope : This is an optional attribute that may be used for specifying the scope of the attribute ‘var’.

Syntax-
<fmt:setTimeZone value=”<string>” var=”<string>” scope=”<string>”/>
 
The following JSP code displays the current date using the default time zone and assigns the time zone to the GMT-8 using the <fmt:setTimeZone> tag. Also displays the date using the newly set time zone.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;fmt:setTimeZone&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;fmt:setTimeZone&gt; Demo</h1>
    <c:set var="today" value="<%=new java.util.Date()%>" />
    <p>Date in the current time zone: 
    <strong>
      <fmt:formatDate value="${today}" type="both" timeStyle="long" dateStyle="long" />
    </strong></p>
    <fmt:setTimeZone value="GMT-8" />
    <p>Date in the GMT-8 time zone: 
    <strong>
      <fmt:formatDate value="${today}" type="both" timeStyle="long" dateStyle="long" />
    </strong></p>
  </body>
</html>


Output-

<<Previous <<   || Index ||   >>Next >>
Previous
Next

One Response

  1. Sankar lp October 25, 2013