如何在JSTL中格式化日期 [英] How to format date in JSTL

查看:149
本文介绍了如何在JSTL中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,通过我们网站上的所有新闻。其中一个字段是日期 $ {newsitem.value ['Date']} ,以毫秒为单位。我想在网页上以月/日/年格式显示此日期。我认为JSTL格式标签< fmt:formatDate> 将有所帮助,但我没有成功。你知道怎么做吗

 < cms:contentaccess var =newsitem/> 
< h2>< c:out value =$ {newsitem.value ['Title']}/>< / h2>
//显示日期
< c:out value =$ {newsitem.value ['Text']}escapeXml =false/>


解决方案

是的JSTL formatDate 标签应该将此工作与将Timestamp值更改为日期对象(需要解决您的评论中提到的异常有关)。



确保您在JSP声明中正确定义了 fmt 前缀

 <%@ taglib uri =http://java.sun.com/jsp/jstl/fmtprefix =fmt%> 

渲染输出,首先将时间戳转换为日期值。我正在使用 yyyy-MM-dd 作为格式模式 dateFormat 标签支持其他格式选项

 < cms:contentaccess var =newsitem/> 
< jsp:useBean id =newsDateclass =java.util.Date/>
< jsp:setProperty name =newsDateproperty =timevalue =$ {newsitem.value ['Date']}/>
< h2>< c:out value =$ {newsitem.value ['Title']}/>< / h2>
< fmt:formatDate pattern =yyyy-MM-ddvalue =$ {newsDate}/>
< c:out value =$ {newsitem.value ['Text']}escapeXml =false/>


I have a loop that goes through all the news items we have on our site. One of the fields is date ${newsitem.value['Date']}, given in millliseconds. I'd like to display this date in month/day/year format on the webpage. I thought JSTL format tag, <fmt:formatDate>, would help, but I haven't succeeded. Do you know how to do it?

<cms:contentaccess var="newsitem" />
<h2><c:out value="${newsitem.value['Title']}" /></h2>
// display date here        
<c:out value="${newsitem.value['Text']}"  escapeXml="false" />

解决方案

Yes the JSTL formatDate tag should do the job in combination with changing the Timestamp value into a date object (which is required to work around the exception mentioned in your comment).

Ensure that you have properly defined the fmt prefix in the JSP declarations

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

Render the output, convert the time stamp to a date value first. I'm using yyyy-MM-dd as the format pattern, the dateFormat tag supports other formatting options as well.

<cms:contentaccess var="newsitem" />
<jsp:useBean id="newsDate" class="java.util.Date" />
<jsp:setProperty name="newsDate" property="time" value="${newsitem.value['Date']}" />
<h2><c:out value="${newsitem.value['Title']}" /></h2>
<fmt:formatDate pattern="yyyy-MM-dd" value="${newsDate}" />
<c:out value="${newsitem.value['Text']}" escapeXml="false" />

这篇关于如何在JSTL中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆