日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

011_SpringBoot视图层技术thymeleaf-日期格式化

發布時間:2025/5/22 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 011_SpringBoot视图层技术thymeleaf-日期格式化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一. 日期格式化

1. ${#dates.format(birthday)}格式化日期, 默認的以瀏覽器默認語言為格式化標準。

2. ${#dates.format(birthday,'yyy/MM/dd')}按照自定義的格式做日期轉換。

3. ${#dates.year(birthday)}獲取年。

4. ${#dates.month(birthday)}獲取月。

5. ${#dates.day(birthday)}獲取日。

二. Thymeleaf日期格式化案例

1. 使用maven構建SpringBoot的名叫spring-boot-view-thymeleaf-dates項目

2. pom.xml?

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjbs</groupId><artifactId>spring-boot-view-thymeleaf-dates</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html標簽不規范報錯 --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies> </project>

3. 在src/main/resources/templates下新建dates.html

<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>Thymeleaf日期格式化</title></head><body><h3>Thymeleaf格式化日期, 默認的以瀏覽器默認語言為格式化標準</h3><span th:text="${#dates.format(birthday)}"></span><hr/><h3>Thymeleaf按照自定義的格式做日期轉換</h3><span th:text="${#dates.format(birthday,'yyy/MM/dd')}"></span><hr/><h3>Thymeleaf獲取年</h3><span th:text="${#dates.year(birthday)}"></span><hr/><h3>Thymeleaf獲取月</h3><span th:text="${#dates.month(birthday)}"></span><hr/><h3>Thymeleaf獲取日</h3><span th:text="${#dates.day(birthday)}"></span></body> </html>

4. 新建UserController.java

package com.bjbs.controller;import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class UserController {@RequestMapping("/show")public String showInfo(Model model) {model.addAttribute("birthday", new Date());return "dates";} }

5. 新建App.java

package com.bjbs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot啟動類*/ @SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }

6. 啟動項目, 并使用瀏覽器訪問

總結

以上是生活随笔為你收集整理的011_SpringBoot视图层技术thymeleaf-日期格式化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。