java 如何在 JSTL 中显示格式化的日历类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2848894/
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
How to display formatted Calendar type in JSTL?
提问by maectpo
I am using Calendar type in Java and I would like to display formatted date in JSTL.
我在 Java 中使用日历类型,我想在 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
java.text.ParseException:无法解析的日期
Can JSTL format Calendartype?
JSTL 格式Calendar类型可以吗?
回答by BalusC
No, it can only format the java.util.Datetype since it uses DateFormat#format()under the hoods. Use Calendar#getTime()to grab it from the Calendar.
不,它只能格式化java.util.Date类型,因为它DateFormat#format()在引擎盖下使用。用于Calendar#getTime()从Calendar.
<fmt:formatDate value="${photo.dateCreated.time}" type="date" dateStyle="short" />
Note that you need fmt:formatDatefor this, not fmt:parseDate. Formatting is converting a Dateobject into a human readable date string and parsing is converting a human readable date string into a Dateobject.
请注意,您需fmt:formatDate要这样做,而不是fmt:parseDate. 格式化是将Date对象转换为人类可读的日期字符串,解析是将人类可读的日期字符串转换为Date对象。

