Computer >> Computer tutorials >  >> Programming >> Programming

How to parse date in JSP?


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

Attribute

The <fmt:parseDate> tag has the following attributes −

AttributeDescriptionRequiredDefault
ValueDate value to read (parse)NoBody
typeDATE, TIME, or BOTHNodate
dateStyleFULL, LONG, MEDIUM, SHORT, or DEFAULTNoDefault
timeStyleFULL, LONG, MEDIUM, SHORT, or DEFAULTNoDefault
parseLocaleLocale to use when parsing the dateNoDefault locale
patternCustom parsing patternNoNone
timeZoneTime zone of the parsed dateNoDefault time zone
varName of the variable to store the parsed dateNoPrint to page
scopeScope of the variable to store the formatted dateNopage

A pattern attribute is provided that works just like the pattern attribute for the <fmt:formatDate> tag. However, in the case of parsing, the pattern attribute tells the parser what format to expect.

Example

<%@ taglib prefix = "c" uri = "https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fmt" uri = "https://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-2010" />
      <fmt:parseDate value = "${now}" var = "parsedEmpDate" pattern = "dd-MM-yyyy" />
      <p>Parsed Date: <c:out value = "${parsedEmpDate}" /></p>
   </body>
</html>

The above code will generate the following result −

Date Parsing:
Parsed Date: Wed Oct 20 00:00:00 GST 2010