@DateTimeFormat只能工作在有限的模式下。为什么会发生这种情况?

huangapple go评论54阅读模式
英文:

@DateTimeFormat does work only to limited pattern. Why is it happening?

问题

我正在制作一个表单,用于接收两个注册日期,以查找在插入的日期之间注册的用户列表。

为了将表单中的输入绑定到命令实例的 LocalDateTime 类型的数据,我在我的命令实例中使用了 @DateTimeFormat 注解,但是在此代码中只有一个模式起作用。

只有 yyyyMMddHH 模式可以正常工作,没有异常。其他模式如 yyyy-MM-ddyyyy/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 = &quot;yyyyMMddHH&quot;)
	private LocalDateTime from;
	@DateTimeFormat(pattern = &quot;yyyyMMddHH&quot;)
	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

&lt;body&gt;

	&lt;h2&gt;&lt;spring:message code=&quot;regDateSearchFormTitle&quot;/&gt;&lt;/h2&gt;
	
	&lt;spring:message code=&quot;dateFormatInstruction&quot;/&gt;&lt;br/&gt;&lt;br/&gt;
	
	&lt;form:form action=&quot;processSearch&quot; modelAttribute=&quot;searchCommand&quot;&gt;
		
		&lt;label&gt;	
			&lt;form:input path=&quot;from&quot;/&gt;&lt;spring:message code=&quot;from&quot;/&gt;
			&lt;form:errors path=&quot;from&quot;/&gt;
		&lt;/label&gt;
			
		&lt;label&gt;
			&lt;form:input path=&quot;to&quot;/&gt;&lt;spring:message code=&quot;to&quot;/&gt;
			&lt;form:errors path=&quot;to&quot;/&gt;
		&lt;/label&gt;
			
		&lt;input type=&quot;submit&quot; value=&quot;search&quot;/&gt;
	&lt;/form:form&gt;
	
	&lt;c:if test=&quot;${!empty members}&quot;&gt;
		&lt;table&gt;
			&lt;tr&gt;
				&lt;th&gt;&lt;spring:message code=&quot;search.id&quot;/&gt;&lt;/th&gt;
				&lt;th&gt;&lt;spring:message code=&quot;search.name&quot;/&gt;&lt;/th&gt;
				&lt;th&gt;&lt;spring:message code=&quot;search.email&quot;/&gt;&lt;/th&gt;
				&lt;th&gt;&lt;spring:message code=&quot;search.regDate&quot;/&gt;&lt;/th&gt;
			&lt;/tr&gt;

		&lt;c:forEach var=&quot;member&quot; items=&quot;${members}&quot;&gt;
			&lt;tr&gt;
				&lt;td&gt;${member.id}&lt;/td&gt;
				&lt;td&gt;${member.name}&lt;/td&gt;
				&lt;td&gt;${member.email}&lt;/td&gt;
				&lt;td&gt;&lt;tf:formatDateTime value=&quot;${member.registerDateTime}&quot; pattern=&quot;yyyy-MM-dd&quot;/&gt;&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/c:forEach&gt;
		
		&lt;/table&gt;
	&lt;/c:if&gt;
&lt;/body&gt;

答案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 = &quot;yyyy-MM-dd&quot;)
private LocalDate from;
@DateTimeFormat(pattern = &quot;yyyy-MM-dd&quot;)
private LocalDate to;

You can then convert a string with format "2020-08-13" into LocalDate without exception being thrown.

huangapple
  • 本文由 发表于 2020年9月10日 11:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63822565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定