Categories: JSTL

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 >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago