Thymeleaf uses two objects ා dates or ා calendars in the template to process dates, most of which are similar.
Development environment: IntelliJ IDEA 2019.2.2
Spring Boot version: 2.1.8
Create a new Spring Book project named demo.
1,pom.xml
Adding Thymeleaf dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2,src/main/resources/application.yml
Set the template cache to false so that when you refresh the browser after modifying the html page, you can see the results immediately
spring: thymeleaf: cache: false
3,src/main/java/com/example/demo/TestController.java
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.*; @Controller public class TestController { @RequestMapping("/") public String test(Model model){ Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.add(Calendar.DATE, 1); cal2.add(Calendar.DATE, 2); Date[] arr = new Date[]{cal1.getTime(), cal2.getTime()}; List list = Arrays.asList(arr); Set set = new HashSet(list); model.addAttribute("date", date); model.addAttribute("cal", cal); model.addAttribute("arr", arr); model.addAttribute("list", list); model.addAttribute("set", set); return "test"; } }
4,src/main/resources/templates/test.html
<div>format date<div> format(date) <div th:text="${#dates.format(date)}"></div> formatISO(date) <div th:text="${#dates.formatISO(date)}"></div> format(date,'yyyy-MM-dd HH:mm:ss') <div th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}"></div> format(cal) <div th:text="${#calendars.format(cal)}"></div> formatISO(date) <div th:text="${#calendars.formatISO(date)}"></div> format(date,'yyyy-MM-dd HH:mm:ss') <div th:text="${#calendars.format(date,'yyyy-MM-dd HH:mm:ss')}"></div> arrayFormat(arr) <div th:each="date : ${#dates.arrayFormat(arr)}"> <div th:text="${date}"></div> </div> listFormat(list,'yyyy-MM-dd') <div th:each="date : ${#dates.listFormat(list,'yyyy-MM-dd')}"> <div th:text="${date}"></div> </div> setFormat(set) <div th:each="date : ${#dates.setFormat(set)}"> <div th:text="${date}"></div> </div> <div>get date<div> day(date) The next few days <div th:text="${#dates.day(date)}"></div> month(date) Month <div th:text="${#dates.month(date)}"></div> monthName(date) Month name <div th:text="${#dates.monthName(date)}"></div> monthNameShort(date) Abbreviation of month name <div th:text="${#dates.monthNameShort(date)}"></div> year(date) Particular year <div th:text="${#dates.year(date)}"></div> dayOfWeek(date) Days of the week index <div th:text="${#dates.dayOfWeek(date)}"></div> dayOfWeekName(date) What day of the week <div th:text="${#dates.dayOfWeekName(date)}"></div> dayOfWeekNameShort(date) What day of the week is abbreviated as <div th:text="${#dates.dayOfWeekNameShort(date)}"></div> hour(date) Time <div th:text="${#dates.hour(date)}"></div> minute(date) branch <div th:text="${#dates.minute(date)}"></div> second(date) second <div th:text="${#dates.second(date)}"></div> millisecond(date) Millisecond <div th:text="${#dates.millisecond(date)}"></div>
Browser access: http://localhost:8080
Page output:
format date format(date) October 13, 2019 10:35:32 p.m. formatISO(date) 2019-10-13T22:35:32.484+08:00 format(date,'yyyy-MM-dd HH:mm:ss') 2019-10-13 22:35:32 format(cal) October 13, 2019 10:35:32 p.m. formatISO(date) 2019-10-13T22:35:32.484+08:00 format(date,'yyyy-MM-dd HH:mm:ss') 2019-10-13 22:35:32 arrayFormat(arr) October 14, 2019, 10:35:32 p.m. October 15, 2019 10:35:32 p.m. listFormat(list,'yyyy-MM-dd') 2019-10-14 2019-10-15 setFormat(set) October 15, 2019 10:35:32 p.m. October 14, 2019, 10:35:32 p.m. get date day(date) 13 month(date) month 10 Month Name (date) month name October Month Name Short (date) Month Name October year(date) year 2019 Days of Week Index 1 dayOfWeekName(date) What day of the week Sunday dayOfWeekNameShort(date) for short Sunday hour(date) 22 minute(date) score 35 second(date) seconds 32 millisecond(date) milliseconds 484