This post helps you understand and use the <fmt:parseDate> tag in the JSTL format tags library with code example.

The <fmt:parseDate> tag parses the string representation of date or time.

 

JSTL <fmt:parseDate> Syntax:

<fmt:parseDate

   value="<string>"

   type="<string>"

   dateStyle="<string>"

   timeStyle="<string>"

   pattern="<string>"

   timeZone="<string>"

   parseLocale="<string>"

   var="<string>"

   scope="<string>"/>

 

Attributes:

Name

Required

Type

Description

    

value

False

java.lang.String

The date string to be parsed.

type

False

java.lang.String

Determines whether value specified in the value attribute contains a date, time or both.

dateStyle

False

java.lang.String

Specifies date style (SHORT, LONG, FULL, MEDIUM or DEFAULT).

timeStyle

False

java.lang.String

Specifies time style (SHORT, LONG, FULL, MEDIUM or DEFAULT).

pattern

False

java.lang.String

Specifies the pattern on how the date string to be parsed.

timeZone

False

java.lang.String

Time zone to interpret if date string contains any time information.

parseLocale

False

java.lang.String

Locale whose date time formatting will be used to parse the date time.

var

False

java.lang.String

Name of the variable to store the parsed result.

scope

False

java.lang.String

Scope to store the var.

 

JSTL <fmt:parseDate> Example: 

The following JSP code parses a string into date and displays its date value.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>&lt;fmt:parseDate&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;fmt:parseDate&gt; Demo</h1>
     <c:set var="currentDate" value="28-12-2012" />
     <fmt:parseDate value="${currentDate}" var="parsedCurrentDate" pattern="dd-MM-yyyy" />
     <p>Current date after parsing: <c:out value="${parsedCurrentDate}" /></p>
  </body>
</html>

 

Output:

parsedate

Recommended Usage of JSTL <fmt:parseDate> tag:

The <fmt:parseDate>is used to convert string representation of date or time to corresponding date or time values.

 

Other JSTL Format Tags:

bundle  |  formatDate  |  formatNumber  |  message  |  param  |  parseNumber  |  requestEncoding  |  setBundle  |  setLocale  |  setTimeZone  |  timeZone


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment