简体   繁体   English

如何在 Thymeleaf 中将字符串转换为日期?

[英]How to convert a string to date in Thymeleaf?

I have the following input:我有以下输入:

<input name="dateOfOrder" class="form-control" id="dateOfOrder" type="date" th:field="*{{dateOfOrder}}" />

And oh this is the fragment of the DTO into which they are inserted :哦,这是插入它们的 DTO 的片段:

@DateTimeFormat(pattern="yyyy-MM-dd")
private LocalDate dateOfOrder;

Unfortunately I get an exception:不幸的是,我得到了一个例外:

Failed to convert from type [java.lang.String] to type [java.time.LocalDate] from value '2022-06-14';无法从值“2022-06-14”从类型 [java.lang.String] 转换为类型 [java.time.LocalDate]; nested exception is java.lang.IllegalArgumentException: Parse attempt failed from value [2022-06-14].嵌套异常是 java.lang.IllegalArgumentException:解析尝试从值 [2022-06-14] 失败。

I`m not proficient in Thymeleaf and would like some hints on what is wrong here我不精通 Thymeleaf,想知道这里有什么问题

您需要使用 Thymeleaf 格式。

<input name="dateOfOrder" class="form-control" id="dateOfOrder" type="date" th:field="${#dates.format(dateOfOrder, 'yyyy-MM-dd')}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM