Formatting date in Thymeleaf

thymeleaf date format am pm
thymeleaf dates format pattern
java date format
spring boot thymeleaf datepicker
spring boot thymeleaf datepicker example
what is thymeleaf used for
thymeleaf tutorial
thymeleaf table

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

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

Formatting date in Thymeleaf, Bean validation doesn't matter, you should use Thymeleaf formatting: <td th:text="​${#dates.format(sprint.releaseDate, 'dd-MMM-yyyy')}"></td>. How to Work with Dates in Thymeleaf. 1. Introduction. Thymeleaf is a Java template engine designed to work directly with Spring. For an intro to Thymeleaf and Spring, have a look at 2. Maven Dependencies. 3. Old and New: java.util and java.time. 4. Conclusion.

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

thymeleaf, thymeleaf documentation: Format date. Example#. <p> Today: <span th:text="$​{#calendars.format(today,'dd MMMM yyyy')}">30 May 2017</span> </p>. thymeleaf. Getting started with thymeleaf; Expression Utility Objects; Format date; Format decimal; Parsing date; String contains; String length; Externalizing Text in Thymeleaf; Spring Security and Thymeleaf; Using Lists with Thymeleaf

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

You can use:

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

General Usage - Date formatting in form - Thymeleaf, Date formatting in form. Hi. I use Spring MVC and Thymeleaf 2. I need to format dates based on users preferences. Currently I use this : But my  Date formatting in form. Hi. I use Spring MVC and Thymeleaf 2. I need to format dates based on users preferences. Currently I use this : But my problem is that when I submit the form, I

you should use Thymeleaf formatting milliseconds

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

Dates (thymeleaf 2.1.6.RELEASE API), public final class Dates extends Object. Utility class for performing dates ( Date ) operations. An object format. public String format(Date target, String pattern)  Date formatting using #dates.format. I can get this to work: &lt;span th:text="'Added by ' + ${bean.addedBy} + ' on ' + ${#dates.format(bean.addedOn, 'd MMMM yyyy H:mm')}">Added by X on 12 July Thymeleaf - User Forum › General Usage

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

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

Document, Date objects: formatting, component extraction, etc. #calendars : analogous to #​dates , but for java.util.Calendar objects. #numbers :  Date formats not working. Afternoon all, I have been trying to format a date, but have hit a problem and no amount of googling appears to help My underlying object has a property named openDate

HOW-TO: Java 8 Date & Time with Thymeleaf and Spring Boot, If you happen to work with Spring Boot and Thymeleaf and you need to format Java 8 Date & Time objects in your views you may utilize  Formatting the LocalDate is only possible if we will specify only the particular date fields, skipping the time fields. Output result : 24-05-2019 21:57 24-05-2019 21:57 24-05-2019

Format LocalDateTime with Thymeleaf · GitHub, Thymeleaf "extras" dialect for formatting and creating Java 8 Time API objects setFormat(temporalsSet)} /* * Format date with the standard format for the  public final class Dates extends Object. Utility class for performing dates (Date) operations.An object of this class is usually available in variable evaluation expressions with the name #dates.

thymeleaf/thymeleaf-extras-java8time: Thymeleaf "extras , The last thing I tried is approach one using thymeleaf's #dates.format(date) in the text field. Unfortunately this also appends HH:MM:SS, which I don't want. public final class Dates extends Object Utility class for performing dates ( Date ) operations. An object of this class is usually available in variable evaluation expressions with the name #dates .

Comments
  • I'll just leave this here: If you are using LocalDate or LocalDateTime use "temporals" instead of "dates" in Thymeleaf
  • Is there a way to configure the format that the double-bracket converter uses?
  • @DavidTroyer it works with just about any way you would normally do this -- you can use the @DateTimeFormat (just like the question), you can have your @Configuration class extend WebMvcConfigurerAdapter and override addFormatters to add a Converter<Date, String>, etc...
  • Nice answer for internationalization. The view is not dependent on the locale.
  • saved my day :)
  • Notice - temporals supports only java 8 time api (not standard java.util.Date). In order to use this functionality you need to add thymeleaf-extras-java8time dependency.