How to convert Java date to ReadableInstant for Joda Time inside JSP?

I created a java.util.Date object named myDate in my controller and passed it to my JSP, where I have the 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 through the POM file.

However, when I try to access myDate from the JSP as follows:

 <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 Joda JSP tags , I can’t say how can I "convert" my myDate to ReadableInstant or ReadablePartial in the context of this JSP?

+8
java date jsp jodatime
source share
1 answer

The error message is self-learning. JodaTime tags do not accept a Java SE Date instance, but a JodaTime DateTime instance or anything that implements a JodaTime ReadableInstant or ReadablePartial .

You need to convert it before providing it in the form.

 DateTime dateTime = new DateTime(date.getTime()); request.setAttribute("myDate", dateTime); 
+12
source share

All Articles