一. 日期格式化

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-日期格式化相关推荐

  1. 010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作

    一. Thymeleaf变量输出 1. th:text在页面中输出变量. 2. th:value将变量输出到input标签的value中. 二. Thymeleaf字符串操作 1. Thymeleaf ...

  2. 015_SpringBoot视图层技术thymeleaf-URL表达式

    1. url表达式: th:href和th:src. 2. url表达式基本语法: @{}. 3. 绝对路径: <a th:href="@{http://www.baidu.com}& ...

  3. 014_SpringBoot视图层技术thymeleaf-访问域对象

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-scope项目 2. pom.xml <project xmlns="http: ...

  4. 013_SpringBoot视图层技术thymeleaf-迭代遍历

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-each项目 2. pom.xml <project xmlns="http:/ ...

  5. 012_SpringBoot视图层技术thymeleaf-条件判断

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-if-switch项目 2. pom.xml <project xmlns="h ...

  6. 008_SpringBoot视图层技术jsp

    1. 使用maven构建SpringBoot的名叫spring-boot-view-jsp项目 2. pom.xml <project xmlns="http://maven.apac ...

  7. 开发日记——th:field=“*{}“中日期格式化问题

    th:field:"*{}"中日期格式化问题 情景 我在写表单修改时需要通过th:field获取库中的数据,有一个字段是date日期类型,由于插入时没做日期规范,导致读取的日期数据 ...

  8. 【Thymeleaf】格式化日期

    前言 thymeleaf 3.0 spring boot 2.0.0RELEASE 无国际化:格式化日期 <p>Today is: <span th:text="${#ca ...

  9. 服务器渲染技术-Thymeleaf

    1.基本介绍 Thymeleaf 是什么 Thymeleaf是一个现代的服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本 Thymel ...

最新文章

  1. AI发展这一年:不断衍生的技术丑闻与抵制声潮
  2. C++运算符重载(友元函数方式)
  3. 成功解决numpy.linalg.LinAlgError: singular matrix
  4. 【BZOJ4500】矩阵(差分约束)
  5. Unity2017.1官方UGUI文档翻译——Visual Components
  6. 最短路模板:dij,spfa与floyd
  7. 关于数据持久化的一个简介
  8. 自己本地如何玩redis_如何把win10系统本地磁盘图标更改为自己的照片
  9. jQuery.unique引发一个血案
  10. 基于Scikit-Learn进行命名实体识别和分类
  11. [转]js模块化(一)
  12. 2021华为软件精英挑战赛的baseLine,Java版,仅供参考,无核心算法
  13. TiDB数据库架构概述
  14. 供应链服务平台方案:助供应链服务公司实现商品+决策+物流+售后协同办公
  15. new Integer()与 Integer.valueOf()区别和联系
  16. ubuntu20.04搭建lamp环境 +制作网页
  17. Android 来去电监听,电话挂断
  18. 硬盘坏道终结者---低级格式化
  19. wxWidgets中从xrc文件中获取菜单选项的指针
  20. 信息技术用计算机写字的教案,黔教版信息技术三年级下册第9课《用计算机写字》教案1.doc...

热门文章

  1. Duo Labs:五大厂商的笔记本预装软件存在重大安全隐患
  2. Guava中针对集合的 filter和过滤功能
  3. 垂直居中实现方式总结
  4. linux基础(6)-shell编程
  5. HTML5如何重塑O2O用户体验
  6. 模拟STL链表类的实现
  7. solaris10找安装包的地方
  8. C++ limits头文件
  9. 有人顶贴,是对你的肯定
  10. rsync文件同步(linux-linux)