JSTL fmt Tag formatDate Example

JSTL fmt Tag formatDate Example


Posted in : Java Posted on : April 10, 2012 at 6:00 PM Comments : [ 0 ]

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

JSTL fmt Tag formatDate Example

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

In JSP date and/or time can be formatted according to the provided styles and pattern. There is a tag in JSTL fmt tag library <fmt:formatDate> that formats the date and/or time according to the provided styles and pattern.

Attributes of <fmt:formatDate> tag

  • value : This is required attribute that specifies the value of date and/or time to be formatted.
  • type : This is an optional attribute specifies whether the time, the date, or both the time and date part of the provided date are to be formatted.
  • dateStyle : This is an optional attribute that specifies the predefined formatting style of date i.e. how the date part has to be formatted. This attribute follows the java.text.DateFormat semantics. And is applied only when the type attribute is not used or is specified to the "date" or "both" other than it is ignored. dateStyle can be the full, long, medium, short, or default.
  • timeStyle : This is an optional attribute that specifies the predefined formatting style of time i.e. how the time part has to be formatted. This attribute follows the java.text.DateFormat semantics. And is applied only when the type attribute is specified to the "time" or "both" other than it is ignored. timeStyle can be the full,long, medium, short, or default.
  • pattern : This is an optional attribute that specifies the custom formatting pattern of the dates and times.
  • timeZone : This is an optional attribute that may be used for specifying the Time Zone in which to represent the formatted time.
  • var : This is an optional attribute that defines the name of a scoped variable, kept the formatted result as String.
  • 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:formatDate> tag. In this example I have created a HTML page where the required date that has to be formatted will be given then created a JSP page where I have used the <fmt:formatDate> tag of JSTL to format that date.

JstlFmtFormatDate.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Date" %>
<%@ 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:formatDate Example</title>
</head>
<h3>Format Date :</h3>
<c:set var="dt" value="<%=new Date()%>" />
<p>Format 1 : <fmt:formatDate type="both" 
dateStyle="long" timeStyle="long" 
value="${dt}" /></p>
<p>Format 2 : <fmt:formatDate type="date" 
value="${dt}" /></p>
<p>Format 3 : <fmt:formatDate type="both" 
value="${dt}" /></p>
<p>Format 4 : <fmt:formatDate type="both" 
dateStyle="short" timeStyle="short" 
value="${dt}" /></p>
<p>Format 5 : <fmt:formatDate type="time" 
value="${dt}" /></p>
<p>Format 6 : <fmt:formatDate pattern="yyyy-MM-dd" 
value="${dt}" /></p>

</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 JstlFmtFormatDate.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 JstlFmtFormatDate.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 as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics