How to set time zone in a JSP?


The <fmt:setTimeZone> tag is used to copy a time zone object into the specified scoped variable.

Attribute

The <fmt:setTimeZone> tag has the following attributes −

AttributeDescriptionRequiredDefault
ValueTime zone to expose as a scoped or configuration variableYesNone
varName of the variable to store the new time zoneNoReplace default
scopeScope of the variable to store the new time zoneNoPage

Example

<%@ 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:setTimeZone Tag</title>
   </head>
   <body>
      <c:set var = "now" value = "<%=new java.util.Date()%>" />
      <p>Date in Current Zone: <fmt:formatDate value = "${now}" type = "both" timeStyle = "long" dateStyle = "long" /></p>
      <p>Change Time Zone to GMT-8</p>
      <fmt:setTimeZone value = "GMT-8" />
      <p>Date in Changed Zone: <fmt:formatDate value = "${now}" type = "both" timeStyle = "long" dateStyle = "long" /></p>
   </body>
</html>

The above code will generate the following result −

Date in Current Zone: 23 September 2010 15:21:37 GST
Change Time Zone to GMT-8
Date in Changed Zone: 23 September 2010 03:21:37 GMT-08:00

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

465 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements