Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 525
user3411565 Avatar asked Feb 14 '15 14:02

user3411565


People also ask

What is LocalDate NOW () in Java?

now() now() method of a LocalDate class used to obtain the current date from the system clock in the default time-zone. This method will return LocalDate based on system clock with default time-zone to obtain the current date.

What does LocalDate parse () return?

Return value: This method returns LocalTime which is the parsed local date-time.

What method is used to convert a LocalDate to a string?

The toString() method of a LocalDate class is used to get this date as a String, such as 2019-01-01. The output will be in the ISO-8601 format uuuu-MM-dd. Parameters: This method does not accepts any parameters.

Does LocalDate have time Java?

The LocalDate class from the java. time package helps us achieve this. LocalDate is an immutable, thread-safe class. Moreover, a LocalDate can hold only date values and cannot have a time component.


1 Answers

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).

like image 74
user3138997 Avatar answered Nov 07 '22 21:11

user3138997