zoukankan      html  css  js  c++  java
  • springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)--thymeleaf格式化LocalDateTime,LocalDate等JDK8新时间类

    依赖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

    https://stackoverflow.com/questions/44084013/how-to-configure-java8timedialect-manually-in-a-spring-boot-application

  • 相关阅读:
    【论文阅读】A practical algorithm for distributed clustering and outlier detection
    第11组 团队Git现场编程实战
    第11组 团队项目-需求分析报告
    团队项目-选题报告
    第二次结对编程作业
    第11组 团队展示
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    (转)script标签到底该放在哪里
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11635649.html
Copyright © 2011-2022 走看看