adam.shaleen
adam.shaleen

Reputation: 1101

Formatting date in 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:

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

Current output

sprint1 2016-10-04 14:10:42.183

Upvotes: 65

Views: 124759

Answers (6)

Georgios Syngouroglou
Georgios Syngouroglou

Reputation: 19944

Regarding the dependencies,

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.12.RELEASE</version>
</dependency>

If you will use LocalDate, LocalDateTime or any other class of the new Java 8 Date package, then you should add this additional dependency,

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

Regarding the type of your date object, if you use Date,

<td th:text="${#dates.format(sprint.releaseDate, 'dd-MM-yyyy HH:mm')}">30-12-2021 23:59</td>

If you use LocalDate or LocalDateTime,

<td th:text="${#temporals.format(sprint.releaseDate, 'dd-MM-yyyy HH:mm')}">30-12-2021 23:59</td>

There is still always the option to pass an object of DateTimeFormatter in your model properties

// Inside your controller
context.setVariable("df", DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"));
// or
model.addAttribute("df", DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"));

// Then, in your template
<td th:text="${df.format(sprint.releaseDate)}">30-12-2021 23:59</td>

This article may help you further.

Upvotes: 3

Adam111p
Adam111p

Reputation: 3717

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

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

Upvotes: 1

David Gonzalez
David Gonzalez

Reputation: 771

If you want show por example = 20-11-2017

You can use:

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

Upvotes: 19

Amit
Amit

Reputation: 1919

you should use Thymeleaf formatting milliseconds

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

Upvotes: 3

Metroids
Metroids

Reputation: 20487

If you want to use converters in th:text attributes, you have to use double-bracket syntax.

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

(They are automatically applied to th:field attributes)

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

Upvotes: 24

DimaSan
DimaSan

Reputation: 12684

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

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

Also make sure your releaseDate property is java.util.Date.

Output will be like: 04-Oct-2016

Upvotes: 118

Related Questions