JSTL Formatting <fmt:setTimeZone> Tag

In JSTL applications, we can use the <fmt:setTimeZone> tag to set the time zone for date and time formatting. Using this tag we can specify a particular time zone for displaying the dates and times. In this article, we will discuss the Syntax and the parameters of this tag. We will also see its additional attributes table and the example demonstrating the implementation.

Syntax <fmt:setTimeZone> Tag

<fmt:setTimeZone value="timeZoneId" 
var="resultVar"
scope="page|request|session|application
"/>

Parameters

  • value: This is used to set the time zone identifier such as “GMT“.
  • var: This is the optional attribute that is used to store the time zone.
  • scope: This is used to specify the variable’s scope like page, request, session, and application. This is also an optional attribute.

Attributes of JSTL <fmt:setTimeZone> Tag

The table describes attributes present in <fmt:setTimeZone> tag with proper description.

Attribute

Description

Required

Default

value

This is used to set the time zone identifier.

Yes

None

var

This is used to store the time zone result.

No

None

scope

This is used to set the scope of variables as page, request, session, and application.

No

page

Example of JSTL Formatting <fmt:setTimeZone> Tag

In this example, we will discuss how to create any date and time format using JSTL tag <fmt:setTimeZone>.

Java




<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    
<html>
  <head>
      <title>fmt:setTimeZone Tag</title>
  </head>
  <body>
    <fmt:setTimeZone value="America/New_York" />
    <h2>Date and Time Example</h2>
    <p>Current Date and Time: <fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" /></p>
  </body>
</html>


Output:

Tag" height="333" src="/public/files/JSTL_timeZone-660.png" width="660">

Explanation of the above Program:

  • In the above example, <fmt:setTimeZone> tag to set the time zone as America/New_York.
  • Then by using the <fmt:formatDate> tag, we displayed the date and time in the “yyyy-MM-dd HH:mm:ss” pattern which was set by the setTimeZone tag.
  • We are printing the date and time in both time zones using HTML <p> tag.