如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:38:26  来源:igfitidea点击:

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

javajspdatejodatime

提问by Sam Bricket

I instantiated a java.util.Dateobject called myDatein my controllerand 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 标记:myDatecontroller

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

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

当然还有必要的Maven依赖项通过 POM 文件添加到项目中。

However, when I try to access myDatefrom 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 myDateto a ReadableInstantor ReadablePartialin the context of this JSP?

参考Joda Time JSP 标签文档,我不知道我应该如何在这个 JSP 的上下文中“转换” mymyDate到 aReadableInstantReadablePartial

回答by BalusC

The error message is self-explaining. The JodaTime tags doesn't accept a Java SE standard Dateinstance, but a JodaTime DateTimeinstance or whatever implements JodaTime's ReadableInstantor ReadablePartial.

错误消息是不言自明的。JodaTime 标签不接受 Java SE 标准Date实例,而是一个 JodaTimeDateTime实例或任何实现 JodaTimeReadableInstantReadablePartial.

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

您需要在将其提供给视图之前对其进行转换。

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