forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
How to work with dates in Thymeleaf (eugenp#960)
* How to work with dates in Thymeleaf * Fixes in PR for Thymeleaf
- Loading branch information
1 parent
82fc8cf
commit e36d928
Showing
6 changed files
with
94 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters