@DateTimeFormat 只适用于有限的模式。为什么会这样?
@DateTimeFormat does work only to limited pattern. Why is it happening?
我正在制作一个接收两个注册日期的表格,以查找在插入的日期之间注册的用户列表。
为了将表单的输入绑定到 Command 实例的 LocalDateTime
类型的数据,我在我的命令实例中使用了 @DateTimeFormat
注释,但是,此代码中只有一种模式有效.
只有 yyyyMMddHH
模式无一例外地工作。 yyyy-MM-dd
、yyyy/MM/dd
等其他模式不起作用。
他们抛出如下异常
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]
这是命令实例的代码,
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>
由于您尝试仅转换日期“2020-08-13”而没有指定时间,因此您应该使用格式如下的 LocalDate:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate from;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate to;
然后您可以将格式为“2020-08-13”的字符串转换为 LocalDate 而不会抛出异常。
我正在制作一个接收两个注册日期的表格,以查找在插入的日期之间注册的用户列表。
为了将表单的输入绑定到 Command 实例的 LocalDateTime
类型的数据,我在我的命令实例中使用了 @DateTimeFormat
注释,但是,此代码中只有一种模式有效.
只有 yyyyMMddHH
模式无一例外地工作。 yyyy-MM-dd
、yyyy/MM/dd
等其他模式不起作用。
他们抛出如下异常
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]
这是命令实例的代码,
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>
由于您尝试仅转换日期“2020-08-13”而没有指定时间,因此您应该使用格式如下的 LocalDate:
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate from;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate to;
然后您可以将格式为“2020-08-13”的字符串转换为 LocalDate 而不会抛出异常。