【问题标题】:Thymeleaf parse preprocessing String to date and format itThymeleaf 将预处理字符串解析为日期并格式化
【发布时间】:2019-02-08 12:11:33
【问题描述】:

我有包含属性列表的模型属性。

我要格式化的值类似于这个字符串:2012-07-16T00:00:00

我尝试使用

<p th:text="${#temporals.format(${myData.mdProperties.get('completionDate')}, 'dd-MM-yyyy')}"></p>

解析一直失败。然后我想我应该使用自定义方言将字符串转换为日期,但这很复杂。有没有更简单的解决方案?

我尝试根据question 将字符串转换为日期,但失败了:

<p th:text="${#temporals.format(new java.util.Date(${{myData.mdProperties.get('completionDate')}}), 'dd-MM-yyyy')}"></p>

【问题讨论】:

  • 您能否将错误/异常添加到您的问题中?目前还不清楚是什么失败了。

标签: spring-boot thymeleaf date-format


【解决方案1】:

每个表达式中的括号太多。通常,您永远不应该嵌套 ${ ... } 表达式(进行预处理时除外)。

此外,您需要创建一个简单的日期格式来首先解析您的日期。这适用于我的测试用例:

<th:block th:with="sdf = ${new java.text.SimpleDateFormat('yyyy-MM-dd''T''HH:mm:ss')}">
    <p th:text="${#dates.format(sdf.parse(myData.mdProperties.get('completionDate')), 'dd-MM-yyyy')}" />
</th:block>

【讨论】:

  • 我尝试了上述方法。但它给了我一个错误-org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "new java.text.SimpleDateFormat('yyyy-MM-dd''T''HH:mm:ss')
【解决方案2】:

这对我有用,我需要为 input type="datetime-local" 设置 min 属性,也许它可能有用

th:attr="min=${#strings.substring(#dates.formatISO(#dates.createNow()),0,16)}"

那套:min="2022-01-06T19:47"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2011-01-16
    • 2016-05-17
    • 2010-11-23
    • 2019-06-23
    相关资源
    最近更新 更多