JSTL fmt Tag timeZone Example

JSTL fmt Tag timeZone Example


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

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

JSTL fmt Tag timeZone Example

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

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

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.

Example :

An example is being given below will demonstrate you about the use of JSTL fmt <fmt:timeZone> 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 use the time zone so, I have used the <fmt:timeZone> tag of JSTL fmt tag library that specifies 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.

JstlFmtTimeZone.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:timeZone 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:timeZone value="US/Eastern">
<td><fmt:formatDate value="${dt}" type="both"/></td>
</fmt:timeZone>
</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 JstlFmtTimeZone.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 JstlFmtTimeZone.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