开发者

How to display formatted Calendar type in JSTL?

开发者 https://www.devze.com 2022-12-30 07:43 出处:网络
I am using Calendar type in Java and I would like to display formatted date in JSTL. I was trying: <%@ taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\" %>

I am using Calendar type in Java and I would like to display formatted date in JSTL.

I was trying:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<fmt:parseDate var="date" type="date" dateStyle="short" value="${photo.dateCreated}" />
<c:out value="${data}" />

But 开发者_运维问答it doesn't work:

java.text.ParseException: Unparseable date

Can JSTL format Calendar type?


No, it can only format the java.util.Date type since it uses DateFormat#format() under the hoods. Use Calendar#getTime() to grab it from the Calendar.

<fmt:formatDate value="${photo.dateCreated.time}" type="date" dateStyle="short" />

Note that you need fmt:formatDate for this, not fmt:parseDate. Formatting is converting a Date object into a human readable date string and parsing is converting a human readable date string into a Date object.

0

精彩评论

暂无评论...
验证码 换一张
取 消