java 使用 fmt:formatDate 格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4424609/
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
Formatting date with fmt:formatDate
提问by eddy
I trying to format a date using fmt:formatDate.
我试图使用 fmt:formatDate 格式化日期。
<c:forEach items="${list}" var="item">
<tr>
<!--some other columns-->
<td align="left">
<fmt:parseDate value="${item.date}" type="both" var="date"/>
<fmt:formatDate value="${date}" type="both" pattern="dd/MM/yyyy H:m"/>
</td>
<!--some other columns-->
</tr>
</c:forEach>
but I get this exception:
但我得到这个例外:
java.text.ParseException: Unparseable date: "2010-12-12 16:00:00"
java.text.ParseException:无法解析的日期:“2010-12-12 16:00:00”
All I want is to show the var datethis way: 12/12/2010 16:00. How can I achieve this?
我想要的只是以这种方式显示 var日期:12/12/2010 16:00。我怎样才能做到这一点?
Thanks in advance.
提前致谢。
回答by BalusC
You need to specify the parse pattern.
您需要指定解析模式。
<fmt:parseDate value="${item.date}" pattern="yyyy-MM-dd HH:mm:ss" var="date"/>
<fmt:formatDate value="${date}" pattern="dd/MM/yyyy HH:mm" />
Normal practice, however, is to store dates as java.util.Date
, not as java.lang.String
. Then you can just do:
然而,通常的做法是将日期存储为java.util.Date
,而不是java.lang.String
。然后你可以这样做:
<fmt:formatDate value="${item.date}" pattern="dd/MM/yyyy HH:mm" />
Use as much as possible the right type for the value. It will become more easy to handle it.
尽可能使用正确的值类型。它会变得更容易处理。