Thymeleaf对象的使用:日期对象

Thymeleaf在模板中使用 #dates 或 #calendars 两个对象来处理日期,这两个对象大部分类似。

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

1、pom.xml
加入Thymeleaf依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、src/main/resources/application.yml 
设置模板缓存为false,这样修改html页面后刷新浏览器能马上看到结果

spring:
  thymeleaf:
    cache: false

3、src/main/java/com/example/demo/TestController.java

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
public class TestController {
    @RequestMapping("/")
    public String test(Model model){
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.add(Calendar.DATE, 1);
        cal2.add(Calendar.DATE, 2);

        Date[] arr = new Date[]{cal1.getTime(), cal2.getTime()};
        List list = Arrays.asList(arr);
        Set set = new HashSet(list);

        model.addAttribute("date", date);
        model.addAttribute("cal", cal);
        model.addAttribute("arr", arr);
        model.addAttribute("list", list);
        model.addAttribute("set", set);

        return "test";
    }
}

4、src/main/resources/templates/test.html

<div>格式化日期<div>
format(date)
<div th:text="${#dates.format(date)}"></div>
formatISO(date)
<div th:text="${#dates.formatISO(date)}"></div>
format(date,'yyyy-MM-dd HH:mm:ss')
<div th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}"></div>
format(cal)
<div th:text="${#calendars.format(cal)}"></div>
formatISO(date)
<div th:text="${#calendars.formatISO(date)}"></div>
format(date,'yyyy-MM-dd HH:mm:ss')
<div th:text="${#calendars.format(date,'yyyy-MM-dd HH:mm:ss')}"></div>
arrayFormat(arr)
<div th:each="date : ${#dates.arrayFormat(arr)}">
    <div th:text="${date}"></div>
</div>
listFormat(list,'yyyy-MM-dd')
<div th:each="date : ${#dates.listFormat(list,'yyyy-MM-dd')}">
    <div th:text="${date}"></div>
</div>
setFormat(set)
<div th:each="date : ${#dates.setFormat(set)}">
    <div th:text="${date}"></div>
</div>
<div>获取日期<div>
day(date) 第几天
<div th:text="${#dates.day(date)}"></div>
month(date) 月份
<div th:text="${#dates.month(date)}"></div>
monthName(date) 月份名称
<div th:text="${#dates.monthName(date)}"></div>
monthNameShort(date) 月份名称简称
<div th:text="${#dates.monthNameShort(date)}"></div>
year(date) 年份
<div th:text="${#dates.year(date)}"></div>
dayOfWeek(date) 星期几索引
<div th:text="${#dates.dayOfWeek(date)}"></div>
dayOfWeekName(date) 星期几名称
<div th:text="${#dates.dayOfWeekName(date)}"></div>
dayOfWeekNameShort(date) 星期几名称简称
<div th:text="${#dates.dayOfWeekNameShort(date)}"></div>
hour(date) 时
<div th:text="${#dates.hour(date)}"></div>
minute(date) 分
<div th:text="${#dates.minute(date)}"></div>
second(date) 秒
<div th:text="${#dates.second(date)}"></div>
millisecond(date) 毫秒
<div th:text="${#dates.millisecond(date)}"></div>

浏览器访问:http://localhost:8080
页面输出:

格式化日期
format(date)
2019年10月13日 下午10时35分32秒
formatISO(date)
2019-10-13T22:35:32.484+08:00
format(date,'yyyy-MM-dd HH:mm:ss')
2019-10-13 22:35:32
format(cal)
2019年10月13日 下午10时35分32秒
formatISO(date)
2019-10-13T22:35:32.484+08:00
format(date,'yyyy-MM-dd HH:mm:ss')
2019-10-13 22:35:32
arrayFormat(arr)
2019年10月14日 下午10时35分32秒
2019年10月15日 下午10时35分32秒
listFormat(list,'yyyy-MM-dd')
2019-10-14
2019-10-15
setFormat(set)
2019年10月15日 下午10时35分32秒
2019年10月14日 下午10时35分32秒
获取日期
day(date) 第几天
13
month(date) 月份
10
monthName(date) 月份名称
十月
monthNameShort(date) 月份名称简称
十月
year(date) 年份
2019
dayOfWeek(date) 星期几索引
1
dayOfWeekName(date) 星期几名称
星期日
dayOfWeekNameShort(date) 星期几名称简称
星期日
hour(date) 时
22
minute(date) 分
35
second(date) 秒
32
millisecond(date) 毫秒
484

猜你喜欢

转载自blog.csdn.net/gdjlc/article/details/102539339
今日推荐