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-日期格式化相关推荐
- 010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作
一. Thymeleaf变量输出 1. th:text在页面中输出变量. 2. th:value将变量输出到input标签的value中. 二. Thymeleaf字符串操作 1. Thymeleaf ...
- 015_SpringBoot视图层技术thymeleaf-URL表达式
1. url表达式: th:href和th:src. 2. url表达式基本语法: @{}. 3. 绝对路径: <a th:href="@{http://www.baidu.com}& ...
- 014_SpringBoot视图层技术thymeleaf-访问域对象
1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-scope项目 2. pom.xml <project xmlns="http: ...
- 013_SpringBoot视图层技术thymeleaf-迭代遍历
1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-each项目 2. pom.xml <project xmlns="http:/ ...
- 012_SpringBoot视图层技术thymeleaf-条件判断
1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-if-switch项目 2. pom.xml <project xmlns="h ...
- 008_SpringBoot视图层技术jsp
1. 使用maven构建SpringBoot的名叫spring-boot-view-jsp项目 2. pom.xml <project xmlns="http://maven.apac ...
- 开发日记——th:field=“*{}“中日期格式化问题
th:field:"*{}"中日期格式化问题 情景 我在写表单修改时需要通过th:field获取库中的数据,有一个字段是date日期类型,由于插入时没做日期规范,导致读取的日期数据 ...
- 【Thymeleaf】格式化日期
前言 thymeleaf 3.0 spring boot 2.0.0RELEASE 无国际化:格式化日期 <p>Today is: <span th:text="${#ca ...
- 服务器渲染技术-Thymeleaf
1.基本介绍 Thymeleaf 是什么 Thymeleaf是一个现代的服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本 Thymel ...
最新文章
- AI发展这一年:不断衍生的技术丑闻与抵制声潮
- C++运算符重载(友元函数方式)
- 成功解决numpy.linalg.LinAlgError: singular matrix
- 【BZOJ4500】矩阵(差分约束)
- Unity2017.1官方UGUI文档翻译——Visual Components
- 最短路模板:dij,spfa与floyd
- 关于数据持久化的一个简介
- 自己本地如何玩redis_如何把win10系统本地磁盘图标更改为自己的照片
- jQuery.unique引发一个血案
- 基于Scikit-Learn进行命名实体识别和分类
- [转]js模块化(一)
- 2021华为软件精英挑战赛的baseLine,Java版,仅供参考,无核心算法
- TiDB数据库架构概述
- 供应链服务平台方案:助供应链服务公司实现商品+决策+物流+售后协同办公
- new Integer()与 Integer.valueOf()区别和联系
- ubuntu20.04搭建lamp环境 +制作网页
- Android 来去电监听,电话挂断
- 硬盘坏道终结者---低级格式化
- wxWidgets中从xrc文件中获取菜单选项的指针
- 信息技术用计算机写字的教案,黔教版信息技术三年级下册第9课《用计算机写字》教案1.doc...