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.


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

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


<%@ taglib uri = "" prefix = "c" %>
<%@ taglib uri = "" prefix = "fmt" %>
      <title>JSTL fmt:setTimeZone Tag</title>
      <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>

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


Kickstart Your Career

Get certified by completing the course

Get Started