thymeleaf 日期格式化
时间: 2023-08-31 07:11:12 浏览: 295
在Thymeleaf中,你可以使用#dates来格式化日期。您可以使用以下语法来格式化日期:
1. 使用默认格式:[[${#dates.format(date)}]] 或 th:text="${#dates.format(date)}"
2. 使用ISO格式:[[${#dates.formatISO(date)}]] 或 th:text="${#dates.formatISO(date)}"
3. 自定义格式:[[${#dates.format(date, 'yyyy-MM-dd HH:mm:ss')}]] 或 th:text="${#dates.format(date, 'yyyy-MM-dd HH:mm:ss')}"
此外,还可以使用#dates对象来获取日期字段的值,如:
1. 获取当前的年份:[[${#dates.year(date)}]]
2. 获取当前的月份:[[${#dates.month(date)}]]
3. 获取当月的天数:[[${#dates.day(date)}]]
4. 获取当前的小时:[[${#dates.hour(date)}]]
5. 获取当前的分钟:[[${#dates.minute(date)}]]
6. 获取当前的秒数:[[${#dates.second(date)}]]
7. 获取当前的毫秒:[[${#dates.millisecond(date)}]]
8. 获取当前的月份名称:[[${#dates.monthName(date)}]]
9. 获取当前是星期几:[[${#dates.dayOfWeek(date)-1}]]
请注意,上述代码中的date表示日期对象的变量名。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [在Thymeleaf中格式化日期](https://blog.csdn.net/allway2/article/details/128031023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [thymeleaf如何格式化日期?](https://blog.csdn.net/qq_19309473/article/details/125041273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [thymeleaf-extras-java8time:Thymeleaf“ extras”方言,用于格式化和创建Java 8 Time API对象](https://download.csdn.net/download/weixin_42127754/18959646)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文