Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Format date/time using tag of jstl 
 

Here in this section we will explain how to format date/time as per user requirment by using tag of JSTL.

 

Format date/time using <fmt:formatDate> tag of jstl

                         

Here in this section we will explain how to format date/time as per user requirment by using <fmt:formatDate> tag of JSTL.

 

 

 

 

 

 

 

<fmt:formatDate> : This tag is used to format date and time according to the user supplied style and format.  
Attributes of the tag <fmt:formatDate>
value  supplied date and time to be formatted.
type  specifies that time or date or both date and time to be formatted of given date/time. 
dateStyle  predefined styles to represent date. For example- default, short, long, full etc
timeStyle   predefined styles to represent time. For example- default, short, long, full etc
pattern  provided by user in what pattern user want to format. For example: dd-mm-yyyy
timeZone  in which time zone user want to represent date/time.
var  var is a variable that is used to store created data source.
Scope  Define the scope for declared variable like page or request or session or application.


dateFormat_JstlFmtTag.jsp

<%@ page import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta http-equiv="refresh" content="3">
<title>Format Date</title>
</head>
<body bgcolor="#6E6E6E">
<%Date date = new java.util.Date();
			pageContext.setAttribute("date", date);%>
<c:set var="date" value="${date}" />
<table bgcolor="#D8D8D8" border="1" width="70%">
	<tr>
		<td width="100%" colspan="2" bgcolor="#1C1C1C">
		<p align="center"><b> <font color="#D8D8D8" size="4">Formatting: 
                 <fmt:formatDate value="${date}" type="both" timeStyle="long" 
                  dateStyle="long" /> 
                </font>
		</b></p>
		</td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="date"</td>
		<td width="49%"><fmt:formatDate type="date" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="time"</td>
		<td width="49%"><fmt:formatDate type="time" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="both"</td>
		<td width="49%"><fmt:formatDate type="both" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="both" dateStyle="default"
		timeStyle="default"</td>
		<td width="49%"><fmt:formatDate type="both" dateStyle="default"
			timeStyle="default" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="short" dateStyle="default"
		timeStyle="short"</td>
		<td width="49%"><fmt:formatDate type="both" dateStyle="short"
			timeStyle="short" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="both" dateStyle="medium"
		timeStyle="medium"</td>
		<td width="49%"><fmt:formatDate type="both" dateStyle="medium"
			timeStyle="medium" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="both" dateStyle="long"
		timeStyle="long"</td>
		<td width="49%"><fmt:formatDate type="both" dateStyle="long"
			timeStyle="long" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : type="both" dateStyle="full"
		timeStyle="full"</td>
		<td width="49%"><fmt:formatDate type="both" dateStyle="full"
			timeStyle="full" value="${date}" /></td>
	</tr>
	<tr>
		<td width="51%">Attribute : pattern="yyyy-MM-dd"</td>
		<td width="49%"><fmt:formatDate pattern="yyyy-MM-dd" value="${date}" />
		</td>
	</tr>
	<tr>
		<td width="51%">Attribute : pattern="yyyy-MM-dd hh:mm:ss"</td>
		<td width="49%"><fmt:formatDate pattern="yyyy-MM-dd hh:mm:ss"
			value="${date}" /></td>
	</tr>
</table>
</body>
</html>

 

In this code we have used "<meta http-equiv="refresh" content="3">" in the sixth line, that provide auto refresh to this web page after every three seconds.

Steps to run this example :

1:  Download the zip file of code and unzip this file, you will get a folder named  'dateFormat_JstlFmtTag'.
2:  Paste this folder in 'Apache Tomcat 6.0.16-->webapps' or generally in directory 'C:\apache-tomcat-6.0.16\webapps'.
3:  Start tomcat server by click on startup.bat file in 'C:\apache-tomcat-6.0.16\bin'.
4: Open browser and type url 'http://localhost:8080/formatDate_JstlFmtTag/dateFormat_JstlFmtTag.jsp
' or click on this link.

Output of the program :

Download Source Code

                         

» View all related tutorials
Related Tags: c xml library file files stl orm ant form io user sed jstl tag parameter transform this js for add

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

How to format dyanamic date. I tried with the above example and it gave me below exception...

JSPG0227E: Exception caught while translating /dateformat.jsp: /dateformat.jsp(99,21) --> JSPG0124E: Custom tag attribute value cannot be runtime expression. value: "[${date}]"

/Atul


Posted by Atul on Monday, 09.8.08 @ 17:57pm | #78032

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.