【问题标题】:Formatting date in Thymeleaf在 Thymeleaf 中格式化日期
【发布时间】:2017-02-13 02:41:27
【问题描述】:

我是 Java/Spring/Thymeleaf 的新手,所以请忍受我目前的理解水平。我确实查看了this similar question,但无法解决我的问题。

我正在尝试获取简化的日期而不是长日期格式。

// DateTimeFormat annotation on the method that's calling the DB to get date.
@DateTimeFormat(pattern="dd-MMM-YYYY")
public Date getReleaseDate() {
    return releaseDate;
}

​ html:

<table>
    <tr th:each="sprint : ${sprints}">
        <td th:text="${sprint.name}"></td>
        <td th:text="${sprint.releaseDate}"></td>
    </tr>
</table>

电流输出

sprint1 2016-10-04 14:10:42.183

【问题讨论】:

    标签: java spring-boot thymeleaf datetime-format


    【解决方案1】:

    bean验证没关系,你应该使用Thymeleaf格式:

    <td th:text="${#dates.format(sprint.releaseDate, 'dd-MMM-yyyy')}"></td>
    

    还要确保您的 releaseDate 属性为 java.util.Date

    输出将类似于:04-Oct-2016

    【讨论】:

    • 我将把它留在这里:如果您使用 LocalDate 或 LocalDateTime 在 Thymeleaf 中使用“时间”而不是“日期”
    【解决方案2】:

    如果要在 th:text 属性中使用转换器,则必须使用双括号语法。

    <td th:text="${{sprint.releaseDate}}"></td>
    

    (它们会自动应用于 th:field 属性)

    http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#double-bracket-syntax

    【讨论】:

    • 有没有办法配置双括号转换器使用的格式?
    • @DavidTroyer 它几乎可以用任何你通常这样做的方式工作——你可以使用@DateTimeFormat(就像问题一样),你可以让你的@Configuration类扩展WebMvcConfigurerAdapter并覆盖 addFormatters 以添加Converter&lt;Date, String&gt; 等...
    • 国际化的好答案。视图不依赖于语言环境。
    • 与 Date 完美搭配,但不适用于 LocalDateTime。你能帮忙吗?
    【解决方案3】:

    如果您想显示端口示例 = 20-11-2017

    你可以使用:

     th:text="${#temporals.format(notice.date,'dd-MM-yyyy')}
    

    【讨论】:

    • 注意 - temporals 仅支持 java 8 time api(不是标准的 java.util.Date)。要使用此功能,您需要添加 thymeleaf-extras-java8time 依赖项。
    • spring-boot-starter-thymeleaf 已经包含thymeleaf-extras-java8time
    【解决方案4】:

    你应该使用 Thymeleaf 格式化毫秒

    <td th:text="${#dates.format(new java.util.Date(transaction.documentDate), 'dd-MMM-yy')}"></td>
    

    【讨论】:

      【解决方案5】:

      关于依赖,

      <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf</artifactId>
          <version>3.0.12.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf-spring5</artifactId>
          <version>3.0.12.RELEASE</version>
      </dependency>
      

      如果您将使用 LocalDateLocalDateTime 或新 Java 8 Date 包的任何其他类,那么您应该添加这个额外的依赖项,

      <dependency>
          <groupId>org.thymeleaf.extras</groupId>
          <artifactId>thymeleaf-extras-java8time</artifactId>
          <version>3.0.4.RELEASE</version>
      </dependency>
      

      关于你的日期对象的类型,如果你使用Date

      <td th:text="${#dates.format(sprint.releaseDate, 'dd-MM-yyyy HH:mm')}">30-12-2021 23:59</td>
      

      如果您使用LocalDateLocalDateTime

      <td th:text="${#temporals.format(sprint.releaseDate, 'dd-MM-yyyy HH:mm')}">30-12-2021 23:59</td>
      

      仍然可以选择在模型属性中传递 DateTimeFormatter 对象

      // Inside your controller
      context.setVariable("df", DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"));
      // or
      model.addAttribute("df", DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"));
      
      // Then, in your template
      <td th:text="${df.format(sprint.releaseDate)}">30-12-2021 23:59</td>
      

      This article 可以进一步帮助您。

      【讨论】:

        【解决方案6】:

        th:text="${#calendars.format(store.someDate(),'dd MMMM yyyy')}"

        API:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#calendars

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-20
          • 2019-01-29
          • 2021-01-23
          • 2011-11-04
          • 2017-12-10
          • 2014-10-22
          • 1970-01-01
          相关资源
          最近更新 更多