Open In App

JSTL Formatting <fmt:timeZone> Tag

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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,

  • value: This is used to set the time zone identifier.
  • var: This is the variable that is used to store the time zone. This is the optional field.
  • scope: This attribute is used to set the scope of the variable which is also an optional attribute.

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>.

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>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:

Output of Examples of JSTL Formatting <fmt:timeZone> Tag

Explanation of the above Program:

  • In the above example, we have created the variable date which mainly consists of the current date and tie using java.util.Date() object.
  • Then, we are specifying the time zone as GMT using the <fmt:timeZone> tag.
  • We are also formatting the date using specified time and date styles.
  • Lastly, we are displaying the date and time using the default time zone and the GMT zone too.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads