Open In App

JSTL Formatting Tag

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JSTL, the formatting <fmt:parseDate> tag is used to parse the date strings into proper java.util.Date objects. This is useful if we need to represent a string in a formatted Date object. Using the tag the tasks of date parsing and formatting can be easily done in Java Server Pages applications.

In this article, we will see the complete information about this tag by exploring the syntax, parameters, attributes, and practical implementation in terms of examples.

Syntax

<fmt:parseDate value="yourDateString" 
pattern="yourDatePattern"
var="parsedDate"
/>

Where,

  • value: This is the date string we need to parse using the parseDate tag in JSTL.
  • pattern: This is the custom pattern that is used for parsing the date string into the Date object.
  • var: This is the variable that is used to store the parsed result and can be used later.

Attributes:

The below table describes all the attributes present in the fmt:parseDate JSTL tag.

Attribute

Description

Required

Default

value

This is the string date value which is to be parsed

Yes

N/A

type

This is the type which can be DATE, TIME, or BOTH

No

N/A

dateStyle

FULL, LONG, MEDIUM, SHORT, DEFAULT.

No

N/A

timeStyle

FULL, LONG, MEDIUM, SHORT, DEFAULT.

No

N/A

parseLocale

This is the locale which is used when parsing the date

No

Default Locale

pattern

This is used to specify the custom pattern for parsing.

No

N/A

timeZone

This is used to specify the time zone of the parsed date.

No

Default Time zone

var

This is the variable to store the parsed date result.

No

N/A

scope

This is used to specify the scope variable to store the format.

No

Page

Example of JSTL Formatting <fmt:parseDate> Tag

This is a example of JavaServer Pages (JSP) code that demonstrates the use of JSTL (JavaServer Pages Standard Tag Library) to format a numeric value using the fmt:parseDate tag.

HTML




<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ 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 Example</title>
   </head>
   <body>
      <h3>Date Parsing:</h3>
      <c:set var="today" value="<%= new java.text.SimpleDateFormat('dd-MM-yyyy').format(new java.util.Date()) %>" />
      <fmt:parseDate value="${today}" var="parsedEmpDate" pattern="dd-MM-yyyy" />
      <p>Parsed Date: <c:out value="${parsedEmpDate}" /></p>
   </body>
</html>


Output:

Output_JSTL_parseDate_Example

Explanation of the Above Program:

In this example, firstly, the current date is been stored in the today variable. This value is in string type. Then we are performing the parsing of the date stored today using the pattern “dd-MM-yyyy” pattern. We are storing the parsed date in the varibale “parsedEmpDate“. We are lastly displaying these results in web browsers using <h1> and <p> tags.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads