springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)
Posted passedbylove
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)相关的知识,希望对你有一定的参考价值。
依赖maven包
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency> <dependency> <groupId>nz.net.ultraq.thymeleaf</groupId> <artifactId>thymeleaf-layout-dialect</artifactId> <version>2.4.1</version> </dependency>
import nz.net.ultraq.thymeleaf.LayoutDialect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; @Configuration public class TemplateConfiguration @Bean public Java8TimeDialect java8TimeDialect() return new Java8TimeDialect(); @Bean public LayoutDialect layoutDialect() return new LayoutDialect();
thymeleaf中格式化方法
<td th:text="$#temporals.format(bean.createTime, ‘yyyy-MM-dd HH:mm:ss‘, locale)"></td>
格式化效果:
官方提供的格式化方法:
/* * ============================================================================= * See javadoc API for class org.thymeleaf.extras.java8time.expression.Temporals * ============================================================================= */ /* * Format date with the standard locale format * Also works with arrays, lists or sets */ $#temporals.format(temporal) $#temporals.arrayFormat(temporalsArray) $#temporals.listFormat(temporalsList) $#temporals.setFormat(temporalsSet) /* * Format date with the standard format for the provided locale * Also works with arrays, lists or sets */ $#temporals.format(temporal, locale) $#temporals.arrayFormat(temporalsArray, locale) $#temporals.listFormat(temporalsList, locale) $#temporals.setFormat(temporalsSet, locale) /* * Format date with the specified pattern * SHORT, MEDIUM, LONG and FULL can also be specified to used the default java.time.format.FormatStyle patterns * Also works with arrays, lists or sets */ $#temporals.format(temporal, ‘dd/MMM/yyyy HH:mm‘) $#temporals.arrayFormat(temporalsArray, ‘dd/MMM/yyyy HH:mm‘) $#temporals.listFormat(temporalsList, ‘dd/MMM/yyyy HH:mm‘) $#temporals.setFormat(temporalsSet, ‘dd/MMM/yyyy HH:mm‘) /* * Format date with the specified pattern and locale * Also works with arrays, lists or sets */ $#temporals.format(temporal, ‘dd/MMM/yyyy HH:mm‘, locale) $#temporals.arrayFormat(temporalsArray, ‘dd/MMM/yyyy HH:mm‘, locale) $#temporals.listFormat(temporalsList, ‘dd/MMM/yyyy HH:mm‘, locale) $#temporals.setFormat(temporalsSet, ‘dd/MMM/yyyy HH:mm‘, locale) /* * Format date with ISO-8601 format * Also works with arrays, lists or sets */ $#temporals.formatISO(temporal) $#temporals.arrayFormatISO(temporalsArray) $#temporals.listFormatISO(temporalsList) $#temporals.setFormatISO(temporalsSet) /* * Obtain date properties * Also works with arrays, lists or sets */ $#temporals.day(temporal) // also arrayDay(...), listDay(...), etc. $#temporals.month(temporal) // also arrayMonth(...), listMonth(...), etc. $#temporals.monthName(temporal) // also arrayMonthName(...), listMonthName(...), etc. $#temporals.monthNameShort(temporal) // also arrayMonthNameShort(...), listMonthNameShort(...), etc. $#temporals.year(temporal) // also arrayYear(...), listYear(...), etc. $#temporals.dayOfWeek(temporal) // also arrayDayOfWeek(...), listDayOfWeek(...), etc. $#temporals.dayOfWeekName(temporal) // also arrayDayOfWeekName(...), listDayOfWeekName(...), etc. $#temporals.dayOfWeekNameShort(temporal) // also arrayDayOfWeekNameShort(...), listDayOfWeekNameShort(...), etc. $#temporals.hour(temporal) // also arrayHour(...), listHour(...), etc. $#temporals.minute(temporal) // also arrayMinute(...), listMinute(...), etc. $#temporals.second(temporal) // also arraySecond(...), listSecond(...), etc. $#temporals.nanosecond(temporal) // also arrayNanosecond(...), listNanosecond(...), etc. /* * Create temporal (java.time.Temporal) objects from its components */ $#temporals.create(year,month,day) // return a instance of java.time.LocalDate $#temporals.create(year,month,day,hour,minute) // return a instance of java.time.LocalDateTime $#temporals.create(year,month,day,hour,minute,second) // return a instance of java.time.LocalDateTime $#temporals.create(year,month,day,hour,minute,second,nanosecond) // return a instance of java.time.LocalDateTime /* * Create a temporal (java.time.Temporal) object for the current date and time */ $#temporals.createNow() // return a instance of java.time.LocalDateTime $#temporals.createNowForTimeZone(zoneId) // return a instance of java.time.ZonedDateTime $#temporals.createToday() // return a instance of java.time.LocalDate $#temporals.createTodayForTimeZone(zoneId) // return a instance of java.time.LocalDate /* * Create a temporal (java.time.Temporal) object for the provided date */ $#temporals.createDate(isoDate) // return a instance of java.time.LocalDate $#temporals.createDateTime(isoDate) // return a instance of java.time.LocalDateTime $#temporals.createDate(isoDate, pattern) // return a instance of java.time.LocalDate $#temporals.createDateTime(isoDate, pattern) // return a instance of java.time.LocalDateTime
参考:https://github.com/thymeleaf/thymeleaf-extras-java8time
以上是关于springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)的主要内容,如果未能解决你的问题,请参考以下文章