JSTL fmt Tag setTimeZone Example

JSTL fmt Tag setTimeZone Example


Posted in : Java Posted on : April 11, 2012 at 6:50 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL fmt setTimeZone tag in JSP.

JSTL fmt Tag setTimeZone Example

In this tutorial you will learn about the JSTL fmt setTimeZone tag in JSP.

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

Example :

An example is being given below will demonstrate you about the use of JSTL fmt <fmt:setTimeZone> tag. In this example I have created a JSP page where I have created a new instance of Date and stored it into the variable var="dt" in the JSTL core <c:set var="" value""> tag. Then I have used the <fmt:formatDate> tag of JSTL fmt tag library to display the date. Now since, I have to set the time zone so, I have used the <fmt:setTimeZone> tag of JSTL fmt tag library that sets the time zone according to the specified time zone name (supported by java platform) or an instance of java.util.TimeZone class. Then again used the <fmt:formatDate> tag to display the date after changing the time zone.

JstlFmtSetTimeZone.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.TimeZone" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="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>JSTL fmt:setTimeZone Example</title>
</head>
<body>
<table border="1">
<tr>
<td>Date and time in the current zone</td>
<c:set var="dt" value="<%=new java.util.Date()%>" />
<td><fmt:formatDate value="${dt}" type="both"/></td>
</tr>
<tr>
<td>Date and time in the Changed time zone</td>
<%-- <fmt:setTimeZone value="Etc/GMT+12"/> --%>
<fmt:setTimeZone value="America/Argentina/San_Luis America/Aruba America/Asuncion America/Barbados America/Blanc-Sablon "/>
<td><fmt:formatDate value="${dt}" type="both"/></td>
</tr>
</table>
</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlFmtSetTimeZone.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlFmtSetTimeZone.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When the execution process will be completed successfully you will get the output on your eclipse browser as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics