Thymeleaf中的格式化日期 [英] Formatting date in Thymeleaf

查看:1173
本文介绍了Thymeleaf中的格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java / Spring / Thymeleaf的新手,所以请保持我目前的理解水平。我确实审查了这个类似的问题,但无法解决我的问题。

I'm brand new to Java/Spring/Thymeleaf so please bear with my current level of understanding. I did review this similar question, but wasn't able to solve my problem.

我正在尝试简化日期而不是长日期格式。

I'm trying to get a simplified date instead of the long date format.

// DateTimeFormat annotation on the method that's calling the DB to get date.
@DateTimeFormat(pattern="dd-MMM-YYYY")
public Date getReleaseDate() {
    return releaseDate;
}


html:

​ html:

<table>
    <tr th:each="sprint : ${sprints}">
        <td th:text="${sprint.name}"></td>
        <td th:text="${sprint.releaseDate}"></td>
    </tr>
</table>

当前输出

sprint1 2016-10-04 14:10:42.183


推荐答案

Bean验证无关紧要,您应该使用Thymeleaf格式:

Bean validation doesn't matter, you should use Thymeleaf formatting:

<td th:text="${#dates.format(sprint.releaseDate, 'dd-MMM-yyyy')}"></td>

还请确保您的 releaseDate 属性为 java.util.Date

输出结果如下: 2016年10月4日

这篇关于Thymeleaf中的格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆