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

user3411565 picture user3411565 · Feb 14, 2015 · Viewed 7.7k times · Source

I haven't been able to figure out how to display a java.time.LocalDate value in a JSP.

In my JSP, I have this:

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


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

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

I'm assuming it's the conversion?

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

Answer

user3138997 picture user3138997 · Feb 14, 2015

I'm assuming it's the conversion?

Yes, it's a conversion related exception.


Solution

You could first use the <fmt:parseDate> action from the JSTL's "I18n capable formatting tag library" to do the conversion and then do the formatting with the <fmt:formatDate> action.

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 right in the "JavaServer Pages™ Standard Tag Library (JSTL)" specification version 1.2 (see page 109).