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?
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.
Return value: This method returns LocalTime which is the parsed local date-time.
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.
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.
I'm assuming it's the conversion?
Yes, it's a conversion related exception.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With