在Thymeleaf格式化日期

问题描述 投票:35回答:4

我是Java / Spring / Thymeleaf的新手,所以请关注我目前的理解水平。我确实审查了this similar question,但无法解决我的问题。

我想要一个简化的日期而不是长日期格式。

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

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
java spring-boot thymeleaf datetime-format
4个回答
63
投票

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

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

还要确保你的releaseDate财产是java.util.Date

输出将是:04-Oct-2016


16
投票

如果要在th:text属性中使用转换器,则必须使用双括号语法。

<td th:text="${{sprint.releaseDate}}"></td>

(它们会自动应用于:字段属性)

http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#double-bracket-syntax


3
投票

如果你想展示例子= 20-11-2017

您可以使用:

 th:text="${#temporals.format(notice.date,'dd-MM-yyyy')}

0
投票

你应该使用Thymeleaf格式化毫秒

<td th:text="${#dates.format(new java.util.Date(transaction.documentDate), 'dd-MMM-yy')}"></td>
© www.soinside.com 2019 - 2024. All rights reserved.