How to work with dates in Thymeleaf (#960)
* How to work with dates in Thymeleaf * Fixes in PR for Thymeleaf
This commit is contained in:
@@ -13,6 +13,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
@@ -74,6 +75,7 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application
|
||||
private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
|
||||
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
|
||||
engine.addDialect(new Java8TimeDialect());
|
||||
engine.setTemplateResolver(templateResolver);
|
||||
return engine;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class DatesController {
|
||||
|
||||
@RequestMapping(value = "/dates", method = RequestMethod.GET)
|
||||
public String getInfo(Model model) {
|
||||
model.addAttribute("standardDate", new Date());
|
||||
model.addAttribute("localDateTime", LocalDateTime.now());
|
||||
model.addAttribute("localDate", LocalDate.now());
|
||||
model.addAttribute("timestamp", Instant.now());
|
||||
return "dates.html";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Baeldung - dates</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Format ISO</h1>
|
||||
<p th:text="${#dates.formatISO(standardDate)}"></p>
|
||||
<p th:text="${#temporals.formatISO(localDateTime)}"></p>
|
||||
<p th:text="${#temporals.formatISO(localDate)}"></p>
|
||||
<p th:text="${#temporals.formatISO(timestamp)}"></p>
|
||||
|
||||
<h1>Format manually</h1>
|
||||
<p th:text="${#dates.format(standardDate, 'dd-MM-yyyy HH:mm')}"></p>
|
||||
<p th:text="${#temporals.format(localDateTime, 'dd-MM-yyyy HH:mm')}"></p>
|
||||
<p th:text="${#temporals.format(localDate, 'MM-yyyy')}"></p>
|
||||
|
||||
<h1>Show only which day of a week</h1>
|
||||
<p th:text="${#dates.day(standardDate)}"></p>
|
||||
<p th:text="${#temporals.day(localDateTime)}"></p>
|
||||
<p th:text="${#temporals.day(localDate)}"></p>
|
||||
|
||||
<h1>Show the name of the week day</h1>
|
||||
<p th:text="${#dates.dayOfWeekName(standardDate)}"></p>
|
||||
<p th:text="${#temporals.dayOfWeekName(localDateTime)}"></p>
|
||||
<p th:text="${#temporals.dayOfWeekName(localDate)}"></p>
|
||||
|
||||
<h1>Show the second of the day</h1>
|
||||
<p th:text="${#dates.second(standardDate)}"></p>
|
||||
<p th:text="${#temporals.second(localDateTime)}"></p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user