Share on Google+Share on Google+

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.


<%@ page import="java.util.*"%>
<%@ taglib uri="" prefix="c"%>
<%@ taglib uri="" prefix="fmt"%>
<meta http-equiv="refresh" content="3">
<title>Format Date</title>
<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%">
<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" /> 
<td width="51%">Attribute : type="date"</td>
<td width="49%"><fmt:formatDate type="date" value="${date}" /></td>
<td width="51%">Attribute : type="time"</td>
<td width="49%"><fmt:formatDate type="time" value="${date}" /></td>
<td width="51%">Attribute : type="both"</td>
<td width="49%"><fmt:formatDate type="both" value="${date}" /></td>
<td width="51%">Attribute : type="both" dateStyle="default"
<td width="49%"><fmt:formatDate type="both" dateStyle="default"
timeStyle="default" value="${date}" /></td>
<td width="51%">Attribute : type="short" dateStyle="default"
<td width="49%"><fmt:formatDate type="both" dateStyle="short"
timeStyle="short" value="${date}" /></td>
<td width="51%">Attribute : type="both" dateStyle="medium"
<td width="49%"><fmt:formatDate type="both" dateStyle="medium"
timeStyle="medium" value="${date}" /></td>
<td width="51%">Attribute : type="both" dateStyle="long"
<td width="49%"><fmt:formatDate type="both" dateStyle="long"
timeStyle="long" value="${date}" /></td>
<td width="51%">Attribute : type="both" dateStyle="full"
<td width="49%"><fmt:formatDate type="both" dateStyle="full"
timeStyle="full" value="${date}" /></td>
<td width="51%">Attribute : pattern="yyyy-MM-dd"</td>
<td width="49%"><fmt:formatDate pattern="yyyy-MM-dd" value="${date}" />
<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>

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


Posted on: August 25, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Format date/time using tag of jstl   View All Comments

Post your Comment

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