在Thymeleaf格式化日期

时间:2016-10-04 19:50:19

标签: java spring-boot thymeleaf datetime-format

我是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

5 个答案:

答案 0 :(得分:64)

Bean验证并不重要,您应该使用Thymeleaf格式:

i=7

另外,请确保您的<td th:text="${#dates.format(sprint.releaseDate, 'dd-MMM-yyyy')}"></td> 媒体资源为releaseDate

输出将如下:java.util.Date

答案 1 :(得分:16)

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

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

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

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

答案 2 :(得分:3)

如果你想要show example = 20-11-2017

您可以使用:

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

答案 3 :(得分:0)

你应该使用Thymeleaf格式化毫秒

<td th:text="${#dates.format(new java.util.Date(transaction.documentDate), 'dd-MMM-yy')}"></td>

答案 4 :(得分:0)

th:text =“ $ {#calendars.format(store.someDate(),'dd MMMM yyyy')}”

API:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#calendars