Categories: JSTL

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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago