Categories: JSTL

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

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on"><i><b>The &lt&semi;fmt&colon;formatDate&gt&semi; tag<&sol;b><&sol;i> is used to format dates in a variety of ways&period; This tag is used to format date and time according to the user supplied style and format&period; <&sol;p>&NewLine;<table border&equals;"0" style&equals;"-webkit-text-stroke-width&colon; 0px&semi; background-color&colon; white&semi; color&colon; black&semi; font-family&colon; Arial&comma; Helvetica&comma; sans-serif&semi; font-size&colon; 13px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24px&semi; orphans&colon; auto&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; auto&semi; width&colon; 67&percnt;px&semi; word-spacing&colon; 0px&semi;">&NewLine;<tbody>&NewLine;<tr>&NewLine;<td bgcolor&equals;"&num;FFECFF" width&equals;"100&percnt;"><b>Attributes of the tag&nbsp&semi;<&sol;b><b>&lt&semi;fmt&colon;formatDate&gt&semi;<&sol;b><b><br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<table bgcolor&equals;"&num;FFFFCC" border&equals;"1" style&equals;"height&colon; 198px&semi; width&colon; 100&percnt;px&semi;">&NewLine;<tbody>&NewLine;<tr>&NewLine;<td height&equals;"38" width&equals;"8&percnt;"><b>value<&sol;b><&sol;td>&NewLine;<td height&equals;"38" width&equals;"92&percnt;">&nbsp&semi;supplied date and time to be formatted&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td height&equals;"19" width&equals;"8&percnt;"><b>type<&sol;b><&sol;td>&NewLine;<td height&equals;"19" width&equals;"92&percnt;">&nbsp&semi;specifies that time or date or both date and time to be formatted of given date&sol;time&period;&nbsp&semi;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td height&equals;"19" width&equals;"8&percnt;"><b>dateStyle<&sol;b><&sol;td>&NewLine;<td height&equals;"19" width&equals;"92&percnt;">&nbsp&semi;predefined styles to represent date&period; For example- default&comma; short&comma; long&comma; full etc<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td height&equals;"19" width&equals;"8&percnt;"><b>timeStyle<&sol;b><&sol;td>&NewLine;<td height&equals;"19" width&equals;"92&percnt;">&nbsp&semi;&nbsp&semi;predefined styles to represent time&period; For example- default&comma; short&comma; long&comma; full etc<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td height&equals;"17" width&equals;"8&percnt;"><b>pattern<&sol;b><&sol;td>&NewLine;<td height&equals;"17" width&equals;"92&percnt;">&nbsp&semi;provided by user in what pattern user want to format&period; For example&colon; dd-mm-yyyy<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td height&equals;"19" width&equals;"8&percnt;"><b>timeZone<&sol;b><&sol;td>&NewLine;<td height&equals;"19" width&equals;"92&percnt;">&nbsp&semi;in which time zone user want to represent date&sol;time&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td height&equals;"19" width&equals;"8&percnt;"><b>var<&sol;b><&sol;td>&NewLine;<td height&equals;"19" width&equals;"92&percnt;">&nbsp&semi;var is a variable that is used to store created data source&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td height&equals;"19" width&equals;"8&percnt;"><b>Scope<&sol;b><&sol;td>&NewLine;<td height&equals;"19" width&equals;"92&percnt;">&nbsp&semi;Define the scope for declared variable like page or request or session or application&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<p><b><br &sol;>&NewLine;<&sol;b> <b>Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; taglib prefix&equals;"c" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jsp&sol;jstl&sol;core" &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"fmt" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jsp&sol;jstl&sol;fmt" &percnt;&gt&semi;&NewLine;&NewLine;&lt&semi;html&gt&semi;&NewLine;&lt&semi;head&gt&semi;&NewLine; &lt&semi;title&gt&semi;JSTL fmt&colon;dateNumber Tag&lt&semi;&sol;title&gt&semi;&NewLine;&lt&semi;&sol;head&gt&semi;&NewLine;&lt&semi;body&gt&semi;&NewLine;&lt&semi;h3&gt&semi;Number Format&colon;&lt&semi;&sol;h3&gt&semi;&NewLine;&lt&semi;c&colon;set var&equals;"date" value&equals;"&lt&semi;&percnt;&equals;new java&period;util&period;Date&lpar;&rpar;&percnt;&gt&semi;" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;table bgcolor&equals;"&num;D8D8D8" border&equals;"1" width&equals;"70&percnt;"&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"100&percnt;" colspan&equals;"2" bgcolor&equals;"&num;1C1C1C"&gt&semi;&NewLine;&lt&semi;p align&equals;"center"&gt&semi;&lt&semi;b&gt&semi; &lt&semi;font color&equals;"&num;D8D8D8" size&equals;"4"&gt&semi;Formatting&colon; &NewLine;&lt&semi;fmt&colon;formatDate value&equals;"&dollar;&lbrace;date&rcub;" type&equals;"both" timeStyle&equals;"long" &NewLine;dateStyle&equals;"long" &sol;&gt&semi; &NewLine;&lt&semi;&sol;font&gt&semi;&NewLine;&lt&semi;&sol;b&gt&semi;&lt&semi;&sol;p&gt&semi;&NewLine;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"date"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"date" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"time"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"time" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"both"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"both" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"both" dateStyle&equals;"default"&NewLine;timeStyle&equals;"default"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"both" dateStyle&equals;"default"&NewLine;timeStyle&equals;"default" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"short" dateStyle&equals;"default"&NewLine;timeStyle&equals;"short"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"both" dateStyle&equals;"short"&NewLine;timeStyle&equals;"short" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"both" dateStyle&equals;"medium"&NewLine;timeStyle&equals;"medium"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"both" dateStyle&equals;"medium"&NewLine;timeStyle&equals;"medium" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"both" dateStyle&equals;"long"&NewLine;timeStyle&equals;"long"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"both" dateStyle&equals;"long"&NewLine;timeStyle&equals;"long" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; type&equals;"both" dateStyle&equals;"full"&NewLine;timeStyle&equals;"full"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate type&equals;"both" dateStyle&equals;"full"&NewLine;timeStyle&equals;"full" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; pattern&equals;"yyyy-MM-dd"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate pattern&equals;"yyyy-MM-dd" value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&NewLine;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td width&equals;"51&percnt;"&gt&semi;Attribute &colon; pattern&equals;"yyyy-MM-dd hh&colon;mm&colon;ss"&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td width&equals;"49&percnt;"&gt&semi;&lt&semi;fmt&colon;formatDate pattern&equals;"yyyy-MM-dd hh&colon;mm&colon;ss"&NewLine;value&equals;"&dollar;&lbrace;date&rcub;" &sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;&sol;table&gt&semi;&NewLine;&lt&semi;&sol;body&gt&semi;&NewLine;&lt&semi;&sol;html&gt&semi;&NewLine;<&sol;pre>&NewLine;<p><b><br &sol;>&NewLine;<&sol;b> <b>This would produce following result&colon;<&sol;b><&sol;p>&NewLine;<div align&equals;"center" id&equals;"ads-id"><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"http&colon;&sol;&sol;50&period;87&period;249&period;200&sol;~dineshon&sol;wp-content&sol;uploads&sol;2013&sol;10&sol;222222&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<p><i><b>&lt&semi;fmt&colon;parseDate&gt&semi; <&sol;b><&sol;i><b>Tag<&sol;b>&&num;8211&semi;<br &sol;>&NewLine;The &lt&semi;fmt&colon;parseDate&gt&semi; tag is used to parse dates&period;<&sol;p>&NewLine;<p> <b>Example-<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; taglib prefix&equals;"c" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jsp&sol;jstl&sol;core" &percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"fmt" uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jsp&sol;jstl&sol;fmt" &percnt;&gt&semi;&NewLine;&NewLine;&lt&semi;html&gt&semi;&NewLine;&lt&semi;head&gt&semi;&NewLine; &lt&semi;title&gt&semi;JSTL fmt&colon;parseDate Tag&lt&semi;&sol;title&gt&semi;&NewLine;&lt&semi;&sol;head&gt&semi;&NewLine;&lt&semi;body&gt&semi;&NewLine;&lt&semi;h3&gt&semi;Date Parsing&colon;&lt&semi;&sol;h3&gt&semi;&NewLine;&lt&semi;c&colon;set var&equals;"now" value&equals;"20-10-2013" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;fmt&colon;parseDate value&equals;"&dollar;&lbrace;now&rcub;" var&equals;"parsedEmpDate" &NewLine; pattern&equals;"dd-MM-yyyy" &sol;&gt&semi;&NewLine;&lt&semi;p&gt&semi;Parsed Date&colon; &lt&semi;c&colon;out value&equals;"&dollar;&lbrace;parsedEmpDate&rcub;" &sol;&gt&semi;&lt&semi;&sol;p&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;body&gt&semi;&NewLine;&lt&semi;&sol;html&gt&semi;&NewLine;<&sol;pre>&NewLine;<p><b>This would produce following result&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"http&colon;&sol;&sol;50&period;87&period;249&period;200&sol;~dineshon&sol;wp-content&sol;uploads&sol;2013&sol;10&sol;333-7&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;10&sol;jstl-parse-number-example&period;html">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;10&sol;jsp-standard-tag-library-jstl-tutorial&period;html">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;10&sol;jstl-bundle-tag&period;html">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jstl-parse-number-example&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jstl-bundle-tag&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '305'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago