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?

This question and answers originated from www.stackoverflow.com
Question by (2/28/2011 2:24:34 AM)

Answer

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);
Answer by

Find More Answers
Related Topics  java  jsp  date  jodatime
Related Questions