现在Java下的时间日期有Date、LocalDateTime、LocalDate、Instant等多种,在thymeleaf中格式化成我们能看懂的日期,都是可以的。
例如有以下几种(Java代码):
ontext context = new Context();
context.setVariable("standardDate", new Date());
context.setVariable("localDateTime", LocalDateTime.now());
context.setVariable("localDate", LocalDate.now());
context.setVariable("timestamp", Instant.now());
在模板中格式化成ISO8601:
<h1>格式化成ISO8601</h1>
<p th:text="${#dates.formatISO(standardDate)}"></p>
<p th:text="${#temporals.formatISO(localDateTime)}"></p>
<p th:text="${#temporals.formatISO(localDate)}"></p>
<p th:text="${#temporals.formatISO(timestamp)}"></p>
格式化成自定义的格式:
<h1>格式化指定的自定义格式</h1>
<p th:text="${#dates.format(standardDate, 'yyyy-MM-dd HH:mm')}"></p>
<p th:text="${#temporals.format(localDateTime, 'yyyy年M月d日 HH:mm')}"></p>
<p th:text="${#temporals.format(localDate, 'yyyy-MM-dd HH:mm:ss')}"></p>
另外还可以提取日期和时间的部分字段(例如提出年份、月份、小时、分钟等等)
示例如下:
${#dates.day(date)}
${#dates.month(date)}
${#dates.monthName(date)}
${#dates.monthNameShort(date)}
${#dates.year(date)}
${#dates.dayOfWeek(date)}
${#dates.dayOfWeekName(date)}
${#dates.dayOfWeekNameShort(date)}
${#dates.hour(date)}
${#dates.minute(date)}
${#dates.second(date)}
${#dates.millisecond(date)}
如果是LocalDate或LocalDateTime类型的:
${#temporals.day(date)}
${#temporals.month(date)}
${#temporals.monthName(date)}
${#temporals.monthNameShort(date)}
${#temporals.year(date)}
${#temporals.dayOfWeek(date)}
${#temporals.dayOfWeekName(date)}
${#temporals.dayOfWeekNameShort(date)}
${#temporals.hour(date)}
${#temporals.minute(date)}
${#temporals.second(date)}
${#temporals.millisecond(date)}
文章评论