Using java.time.LocalDate with JSTL action <fmt: formatDate>

I could not figure out how to display the value of java.time.LocalDate in the JSP.

In my JSP, I have this:

 <fmt:formatDate value="${std.datum}" type="date" pattern="dd.MM.yyyy" var="stdDatum" /> 


The type std.datum is of type java.time.LocalDate . When rendering the JSP, I get this exception:

javax.el.ELException:
Cannot convert 2015-02-14 from the java.time.LocalDate type of the class to the java.util.Date class.

I guess this conversion?

So is it possible to format an instance of the LocalDate class using the <fmr:formatDate> ?

+6
java java-8 jsp jstl jsp-tags
source share
2 answers

I guess this conversion?

Yes, this is an exception related to conversion.


Decision

First, you can use the <fmt:parseDate> from the JSTL <fmt:parseDate> Tag Formatting Library JSTL to perform the conversion, and then format with the <fmt:formatDate> .

Here is an example:

 <fmt:parseDate value="${std.datum}" type="date" pattern="yyyy-MM-dd" var="parsedDate" /> <fmt:formatDate value="${parsedDate}" type="date" pattern="dd.MM.yyyy" var="stdDatum" /> 


This solution is also presented directly in specification 1.2 of the JavaServer Pages β„’ Standard Tag Library (JSTL) (see page 109).

+6
source share

This is an old question, but I believe that in this case it is best to make a custom tld: without any double conversion to and from String.

Make your own tld file, then override the FormatDate class. Finally, declare your own prefix and use custom: formatDate instead of fmt: formatDate.

here is a simplified version

in JSP:

 <%@ taglib uri="/WEB-INF/custom" prefix="custom" %> ... <custom:formatDate value="${std.datum}" pattern="dd/MM/yyyy" /> 

WEB-INF / custom.tld file

 <?xml version="1.0" encoding="UTF-8"?> <tag ib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tag> <description> FormatDate with java8 type </description> <name>formatDate</name> <tag-class>com.custom.tag.FormatDateTag</tag-class> <body-content>empty</body-content> <attribute> <description> Date and/or time to be formatted. </description> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <description> Custom formatting style for dates and times. </description> <name>pattern</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> 

Then the java class tag file

 public class FormatDateTag extends TagSupport { protected Temporal value; protected String pattern; private String var; private int scope; public FormatDateTag() { super (); init (); } private void init() { this.pattern = this.var = null; this.value = null; this.scope = PageContext.PAGE_SCOPE; } public void setVar(final String var) { this.var = var; } public void setScope(final String scope) { this.scope = Util.getScope (scope); } public void setValue(final Temporal value) { this.value = value; } public void setPattern(final String pattern) { this.pattern = pattern; } @Override public int doEndTag() throws JspException { String formatted = null; if (this.value == null) { if (this.var != null) { this.pageContext.removeAttribute (this.var, this.scope); } return EVAL_PAGE; } // Create formatter if (this.pattern != null) { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern (this.pattern); formatted = formatter.format (this.value); } else { // no formatting locale available, use Date.toString() formatted = this.value.toString (); } if (this.var != null) { this.pageContext.setAttribute (this.var, formatted, this.scope); } else { try { this.pageContext.getOut ().print (formatted); } catch (final IOException ioe) { throw new JspTagException (ioe.toString (), ioe); } } return EVAL_PAGE; } @Override public void release() { init (); } } 
+1
source share

All Articles