Open In App

JSTL Formatting <fmt:timeZone> Tag

In JSTL applications, the <fmt:timeZone> tag is mainly used to set the time zone for the data and time formatting. We can set a particular time zone for showing dates and times.The main difference between <fmt:setTimeZone> and <fmt:timeZone> is that, the <fmt:setTimeZone> is used to set the default time zone whereas the <fmt:timeZone> tag is used to set the time zone for the tags within the body.

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 of JSTL <fmt:timeZone> Tag

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

Where,

Attributes of JSTL <fmt:timeZone> Tag

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



Attribute

Description

Required

Default

value

This attribute is used to set the time zone identifier.

Yes

None

var

This is used to store the time zone result. (optional)

No

None

scope

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

No

page

Examples of JSTL Formatting <fmt:timeZone> Tag

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




<%@ 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>JSTL fmt:timeZone Example</title>
</head>
<body>
  <c:set var="date" value="<%=new java.util.Date()%>" />
  <p><b>Date and Time in GMT:</b>
      <fmt:timeZone value="GMT">
          <fmt:formatDate value="${date}" type="both" timeStyle="long" dateStyle="long" />
      </fmt:timeZone>
  </p>
  <p><b>Date and Time using Default Time Zone:</b>
      <fmt:formatDate value="${date}" type="both" timeStyle="long" dateStyle="long" />
  </p>
</body>
</html>

Output:

Explanation of the above Program:


Article Tags :