How to format date in JSTL

天大地大妈咪最大 提交于 2019-12-28 06:46:07

问题


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" />

回答1:


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" />


来源:https://stackoverflow.com/questions/2620676/how-to-format-date-in-jstl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!