英文:
@DateTimeFormat does work only to limited pattern. Why is it happening?
问题
我正在制作一个表单,用于接收两个注册日期,以查找在插入的日期之间注册的用户列表。
为了将表单中的输入绑定到命令实例的 LocalDateTime
类型的数据,我在我的命令实例中使用了 @DateTimeFormat
注解,但是在此代码中只有一个模式起作用。
只有 yyyyMMddHH
模式可以正常工作,没有异常。其他模式如 yyyy-MM-dd
、yyyy/MM/dd
等不起作用。
它们会抛出类似以下的异常:
无法将类型为 java.lang.String 的属性值转换为所需的类型 java.time.LocalDateTime,属性为 from;嵌套异常是 org.springframework.core.convert.ConversionFailedException: 无法将类型 [java.lang.String] 转换为类型 [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime],值为 2020-08-13;嵌套异常是 java.lang.IllegalArgumentException: 解析值 [2020-08-13] 失败
这是命令实例的代码:
public class UserSearchByRegDateRequest {
@DateTimeFormat(pattern = "yyyyMMddHH")
private LocalDateTime from;
@DateTimeFormat(pattern = "yyyyMMddHH")
private LocalDateTime to;
public LocalDateTime getFrom() {
return from;
}
public void setFrom(LocalDateTime from) {
this.from = from;
}
public LocalDateTime getTo() {
return to;
}
public void setTo(LocalDateTime to) {
this.to = to;
}
}
表单的代码:
<body>
<h2><spring:message code="regDateSearchFormTitle"/></h2>
<spring:message code="dateFormatInstruction"/><br/><br/>
<form:form action="processSearch" modelAttribute="searchCommand">
<label>
<form:input path="from"/><spring:message code="from"/>
<form:errors path="from"/>
</label>
<label>
<form:input path="to"/><spring:message code="to"/>
<form:errors path="to"/>
</label>
<input type="submit" value="search"/>
</form:form>
<c:if test="${!empty members}">
<table>
<tr>
<th><spring:message code="search.id"/></th>
<th><spring:message code="search.name"/></th>
<th><spring:message code="search.email"/></th>
<th><spring:message code="search.regDate"/></th>
</tr>
<c:forEach var="member" items="${members}">
<tr>
<td>${member.id}</td>
<td>${member.name}</td>
<td>${member.email}</td>
<td><tf:formatDateTime value="${member.registerDateTime}" pattern="yyyy-MM-dd"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
英文:
I am making a form that receives two registration dates to find the list of the uses who registered between the inserted dates.
To bind the input from the form to the data of the type LocalDateTime
of the Command instance, I use @DateTimeFormat
annotation in my command instance, however, only one pattern works in this code.
Only the yyyyMMddHH
pattern work without an exception. Other patterns such as yyyy-MM-dd
, yyyy/MM/dd
, and more don't work.
They throw an exception like the following
> Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime for property from; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value 2020-08-13; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2020-08-13]
This is the code for the command instance,
public class UserSearchByRegDateRequest {
@DateTimeFormat(pattern = "yyyyMMddHH")
private LocalDateTime from;
@DateTimeFormat(pattern = "yyyyMMddHH")
private LocalDateTime to;
public LocalDateTime getFrom() {
return from;
}
public void setFrom(LocalDateTime from) {
this.from = from;
}
public LocalDateTime getTo() {
return to;
}
public void setTo(LocalDateTime to) {
this.to = to;
}
}
Code for the form
<body>
<h2><spring:message code="regDateSearchFormTitle"/></h2>
<spring:message code="dateFormatInstruction"/><br/><br/>
<form:form action="processSearch" modelAttribute="searchCommand">
<label>
<form:input path="from"/><spring:message code="from"/>
<form:errors path="from"/>
</label>
<label>
<form:input path="to"/><spring:message code="to"/>
<form:errors path="to"/>
</label>
<input type="submit" value="search"/>
</form:form>
<c:if test="${!empty members}">
<table>
<tr>
<th><spring:message code="search.id"/></th>
<th><spring:message code="search.name"/></th>
<th><spring:message code="search.email"/></th>
<th><spring:message code="search.regDate"/></th>
</tr>
<c:forEach var="member" items="${members}">
<tr>
<td>${member.id}</td>
<td>${member.name}</td>
<td>${member.email}</td>
<td><tf:formatDateTime value="${member.registerDateTime}" pattern="yyyy-MM-dd"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
答案1
得分: 2
由于您只想转换日期部分为"2020-08-13",而不包含时间,您应该使用以下格式的LocalDate:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate from;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate to;
这样,您可以将格式为"2020-08-13"的字符串转换为LocalDate,而不会抛出异常。
英文:
Since you're trying to convert only date "2020-08-13" without time specified you should use LocalDate with format as such:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate from;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate to;
You can then convert a string with format "2020-08-13" into LocalDate without exception being thrown.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论