Handle Java 8 date and time API with Thymeleaf with Spring Boot

Overview

Thymeleaf does not have a built-in Utility to operate the Java 8 date and time API by default. Include and use thymeleaf-extras-java8time.

Premise

Modify pom.xml

Add the dependency of thymeleaf-extras-java8time.

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

Register Dialect to DI container

** * From spring-boot 1.4.0, it is no longer necessary to register Dialect in the DI container ***

~~ To be able to use it with Spring Boot, Dialect must be registered in the DI container. I will write an example of creating and registering a dedicated Configuration. ~~

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

Call in template

You will be able to call it as follows.

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

Summary

  1. Add dependency
  2. Register Dialect in DI container
  3. Use in template

reference

Recommended Posts

Handle Java 8 date and time API with Thymeleaf with Spring Boot
[Java] Thymeleaf Basic (Spring Boot)
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
How to call and use API in Java (Spring Boot)
Sample code to parse date and time with Java SimpleDateFormat
[MySQL] [java] Receive date and time
Check date correlation with Spring Boot
[Java] LINE integration with Spring Boot
Implement REST API with Spring Boot and JPA (domain layer)
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
Form and process file and String data at the same time with Spring Boot + Java
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
HTTPS with Spring Boot and Let's Encrypt
Java 8 to start now ~ Date time API ~
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Implement paging function with Spring Boot + Thymeleaf
Run WEB application with Spring Boot + Thymeleaf
Date and time
[Java] Get Json from URL and handle it with standard API (javax.script)
Date and time acquisition method using DateAndTime API
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Create your own Utility with Thymeleaf with Spring Boot
[Java] Article to add validation with Spring Boot 2.3.1.
Create a web api server with spring boot
[Java] Get and display the date 10 days later using the Time API added from Java 8.
[Java] Date / time operations
Download with Spring Boot
Create a portfolio app using Java and Spring Boot
Try using DI container with Laravel and Spring Boot
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
Hello World (REST API) with Apache Camel + Spring Boot 2
Spring Security usage memo: Cooperation with Spring MVC and Boot
[Spring Boot] Get user information with Rest API (beginner)
Spring Boot with Spring Security Filter settings and addictive points
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Customize REST API error response with Spring Boot (Part 2)
[Rails] Precautions when comparing date and time with DateTime
Customize REST API error response with Spring Boot (Part 1)
Attempt to SSR Vue.js with Spring Boot and GraalJS
Connect Spring Boot and Angular type-safely with OpenAPI Generator
[Java / Kotlin] Escape (sanitize) HTML5 support with unbescape [Spring Boot]
[MySQL] [java] Receive date and time
Generate barcode with Spring Boot
Hello World with Spring Boot
Java Config with Spring MVC
Implement GraphQL with Spring Boot
What is the LocalDateTime class? [Java beginner] -Date and time class-
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
Set the date and time from the character string with POI
File upload with Spring Boot
Spring Boot starting with copy
Let's make a simple API with EC2 + RDS + Spring boot ①
Access Web API on Android with Get and process Json (Java for the time being)