我在Java中使用Calendar类型,我想在JSTL中显示格式化日期。
我在尝试:
<%@ 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}" />
但它不起作用:
java.text.ParseException:Unparseable date
JSTL格式Calendar
可以输入吗?
答案 0 :(得分:26)
不,它只能格式化java.util.Date
类型,因为它在引擎盖下使用DateFormat#format()
。使用Calendar#getTime()
从Calendar
抓取它。
<fmt:formatDate value="${photo.dateCreated.time}" type="date" dateStyle="short" />
请注意,您需要fmt:formatDate
,而不是fmt:parseDate
。格式化是将Date
对象转换为人类可读日期字符串,解析是将人类可读日期字符串转换为Date
对象。