Sam Bricket
Sam Bricket

Reputation: 111

How to convert a java Date to a ReadableInstant for Joda Time inside a JSP?

I instantiated a java.util.Date object called myDate in my controller and passed it to my JSP where I have a Joda Time JSP tag configured with this at the top of the page:

<%@taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>

and of course the necessary Maven dependencies added to the project via the POM file.

However, when I try to access myDate from the JSP like this:

<joda:format value="${myDate}" style="SM" />

I get this error:

javax.servlet.jsp.JspException: 
value attribute of format tag must be a 
ReadableInstant or ReadablePartial, was: java.util.Date

Referring to the documentation for the Joda Time JSP tags, I can't tell how I should 'convert' my myDate to a ReadableInstant or ReadablePartial in the context of this JSP?

Upvotes: 11

Views: 15741

Answers (1)

BalusC
BalusC

Reputation: 1109132

The error message is self-explaining. The JodaTime tags doesn't accept a Java SE standard Date instance, but a JodaTime DateTime instance or whatever implements JodaTime's ReadableInstant or ReadablePartial.

You need to convert it before providing it to the view.

DateTime dateTime = new DateTime(date.getTime());
request.setAttribute("myDate", dateTime);

Upvotes: 18

Related Questions