thymeleaf +spring date converting

thymeleaf

Solution

Use Thymeleaf formatting:

<td th:text="${#dates.format(comment.createdAt, 'dd-MM-yyyy HH:mm:ss')}">date</td>

You'll get an output in the following format: `18-Oct-2016 14:44:05`.

`#dates`: methods for `java.util.Date` objects: formatting, component extraction, etc.

To convert your `createdAt` field to `java.util.Date` type use:

Date date = Date.from(java.time.ZonedDateTime.now().toInstant());

Or just use `java.util.Date` type:

private Date createdAt = new Date();

this will set `cheatedAt` to current date.

Also you can add thymeleaf-extras-java8time dependency to your project to work with your `ZonedDateTime` type.

This module adds a `#temporals` object similar to the `#dates` or `#calendars` ones in the Standard Dialect, allowing the formatting and creation of temporal objects from Thymeleaf templates:

Then you can use `ZoneDateTime` with the specified pattern:

${#temporals.format(temporal, 'dd/MM/yyyy HH:mm')}

See more at Thymeleaf - Module for Java 8 Time API compatibility.

Problem

This is my data model. I want to use date from here. I do this in my html: ``` <table th:if="${!commentsInTask.empty}"> <tbody> <tr th:each="Comments : ${commentsInTask}"> <tr th:each="comment : ${Comments}"> <td th:text="${comment.user}">user ...</td> <td th:text="${comment.createdAt}">date ...</td> </tr> </tr> </tbody> </table> ``` but it brings: ``` <table> <tbody> <td>JACK</td> <td>1.476787930289E9</td> </tr> </tr> </tbody> </table> ``` This part is unix timedate: 1.476787930289E9 but in the picture at beginning i posted, you saw. Timme is not that. This is in domain ``` public String getCreatedAtString() { return createdAtString; } public TaskComment setCreatedAtString(String createdAtString) { this.createdAtString = createdAtString; return this; } private ZonedDateTime createdAt = ZonedDateTime.now(); ``` Why cantt i see in date format which in picture at the beginning?

Original source