如何将 Java 日期转换为 JSP 中 Joda 时间的 ReadableInstant?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5137666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a java Date to a ReadableInstant for Joda Time inside a JSP?
提问by Sam Bricket
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:
我实例化了一个在 my 中java.util.Date
调用的对象并将它传递给我的 JSP,在那里我在页面顶部配置了一个 Joda Time JSP 标记:myDate
controller
<%@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.
当然还有必要的Maven
依赖项通过 POM 文件添加到项目中。
However, when I try to access myDate
from the JSP like this:
但是,当我尝试myDate
像这样从 JSP访问时:
<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?
参考Joda Time JSP 标签的文档,我不知道我应该如何在这个 JSP 的上下文中“转换” mymyDate
到 aReadableInstant
或ReadablePartial
?
回答by BalusC
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
.
错误消息是不言自明的。JodaTime 标签不接受 Java SE 标准Date
实例,而是一个 JodaTimeDateTime
实例或任何实现 JodaTimeReadableInstant
或ReadablePartial
.
You need to convert it before providing it to the view.
您需要在将其提供给视图之前对其进行转换。
DateTime dateTime = new DateTime(date.getTime());
request.setAttribute("myDate", dateTime);