In JSTL, 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.

 

JSTL <fmt:setTimeZone> Syntax:

<fmt:setTimeZone value="<string>" var="<string>" scope="<string>"/>

 

Attributes:

Name

Required

Type

Description

value

True

java.lang.String

The required time zone value supported by the Java platform. We can give this value as America/New Yorkor as time zone code like GMT-8. Please see the class java.util.TimeZonefor more information on supported time zones.

var

False

java.lang.String

Name of the variable to store the time zone object of type java.util.TimeZone.

scope

False

java.lang.String

Scope to store the var.

 

JSTL <fmt:setTimeZone> Example:

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:

settimezone

 

Recommended Usage of <fmt:setTimeZone> tag:

The <fmt:setTimeZone> is used to set a time zone which is different from the current time zone.

 

Other JSTL Format Tags:

bundle  |  formatDate  |  formatNumber  |  message  |  param  |  parseDate  |  parseNumber  |  requestEncoding  |  setBundle  |  setLocale  |  timeZone


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment