VOOZH about

URL: https://www.javacodegeeks.com/2015/11/how-to-java-8-date-time-with-thymeleaf-and-spring-boot.html

⇱ HOW-TO: Java 8 Date & Time with Thymeleaf and Spring Boot - Java Code Geeks


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 thymeleaf-extras-java8time – Thymeleaf module for Java 8 Date & Time API.

Adding thymeleaf-extras-java8time to an existing Maven or Gradle based Spring Boot project is as easy as adding a dependency and registering new dialect with a template engine.

For Maven, you add the following dependency to you existing POM:

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

Once you have done it, the next step is to add the dialect to the template engine. With Spring Boot you need to define a bean of type org.thymeleaf.extras.java8time.dialect.Java8TimeDialect in your appliacation context. All beans of type org.thymeleaf.dialect.IDialect are injected into Spring Boot’s ThymeleafAutoConfiguration and added to Thymeleaf’s SpringTemplateEngine automatically.

@SpringBootApplication
public class Application {

 @Bean
 public Java8TimeDialect java8TimeDialect() {
 return new Java8TimeDialect();
 }

 public static void main(String[] args) {
 SpringApplication.run(Application.class);
 }
}

What Java8TimeDialectdoes, it adds a temporalsobject to the context as utility objects during expression evaluations. This means, that it can be used in OGNL or SpringEL expression evaluations:

The time is: <strong th:text="${#temporals.format(now, 'dd/MMM/yyyy HH:mm')}">31/12/2015 15:00</strong>

temporals provide many utility method to work with java.time.Temporal: formatting, accessing properties and creating new objects. For more information about the extension and temporals itself checkout project page on GitHub: thymeleaf-extras-java8time

Note: The Spring Boot and Thymeleaf project setup is described in greater details in this blog post: Spring Boot and Thymeleaf with Maven

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Rafal Borowiec
Rafal Borowiec
November 11th, 2015Last Updated: November 11th, 2015
0 1,040 1 minute read

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz