JSTL ParseDate & FormatDate Example <fmt:parseDate> <fmt:formatDate>

The <fmt:formatDate> tag is used to format dates in a variety of ways. 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.

Example:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
  <title>JSTL fmt:dateNumber Tag</title>
</head>
<body>
<h3>Number Format:</h3>
<c:set var="date" value="<%=new java.util.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>

This would produce following result:


<fmt:parseDate> Tag-
The <fmt:parseDate> tag is used to parse dates.

Example-

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
  <title>JSTL fmt:parseDate Tag</title>
</head>
<body>
<h3>Date Parsing:</h3>
<c:set var="now" value="20-10-2013" />

<fmt:parseDate value="${now}" var="parsedEmpDate" 
                              pattern="dd-MM-yyyy" />
<p>Parsed Date: <c:out value="${parsedEmpDate}" /></p>

</body>
</html>


This would produce following result:




<<Previous <<   || Index ||   >>Next >>








No comments:

Post a Comment