JSTL fmt Tag parseDate Example

JSTL fmt Tag parseDate Example


Posted in : Java Posted on : April 10, 2012 at 5:54 PM Comments : [ 0 ]

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

JSTL fmt Tag parseDate Example

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

In JSP a string format of date and/or time is parsed using the JSTL fmt tag library <fmt:parseDate> tag.

Attributes of <fmt:parseDate>

  • value : This is an optional attribute. This attribute specifies the value of string to be parsed.
  • type : This is an optional attribute specifies the type of date string is required to be parsed i.e. date, time, or both.
  • dateStyle : This is an optional attribute that specifies the style of date i.e. how the date part of date string has to be parsed. This attribute 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 style of time i.e. how the time part of date string has to be parsed. This attribute 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 date string is to be parsed.
  • timeZone : This is an optional attribute that may be used for specifying the Time Zone in which to interpret any time information in the date string.
  • parseLocale : This is an optional attribute that specifies Locale of which default formatting pattern for date and times are to be used on the parse operation or, the pattern that is defined through the pattern attribute is used.
  • var : This is an optional attribute that defines the name of a scoped variable, kept the parsed result in the java.util.Date 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:parseDate> tag. In this example I have created a HTML page where the date string that has to be parsed will be given then created a JSP page where I have used the <fmt:parseDate> tag of JSTL to parse that date string, into a specified pattern defined by the attribute of this tag pattern.

parseDate.html

<!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>Parse Date Example</title>
</head>
<body>
<form action="JstlFmtParseDate.jsp">
<table>
<tr>
<td>Enter Date</td>
<td><input type="text" name="date"/></td>
</tr>
<tr>
<td></td><td>(dd-MM-yyyy)</td>
</tr>
<tr>
<td></td><td><input type="submit" value="submit"/>
</table>
</form>
</body>
</html>

JstlFmtParseDate.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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:parseDate Example</title>
</head>
<body>
<table>
<tr>
<td>Parsed Date =</td>
<td><fmt:parseDate value="${param.date}" pattern="dd-MM-yyyy" type="date"/></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 JstlFmtParseDate.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 JstlFmtParseDate.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 you will execute the HTML page following page will be displayed for you :

When you will enter the appropriate value and will click on submit button then output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics