无法将 java.lang.String 类型的属性值转换为所需的 java.util.Date 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23702041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:48:12  来源:igfitidea点击:

Failed to convert property value of type java.lang.String to required type java.util.Date

javaspringdatedata-bindingdate-format

提问by Joe

I'm getting this error when I try to input a date in a form. enter image description here

当我尝试在表单中输入日期时出现此错误。 在此处输入图片说明

TaskController

任务控制器

@RequestMapping(value = "/docreatetask", method = RequestMethod.POST)
public String doCreateTask(Model model, @Valid Task task,
        BindingResult result, Principal principal,
        @RequestParam(value = "delete", required = false) String delete) {
    System.out.println(">TaskController doCreateTask " + task);

    if (result.hasErrors()) {
        System.out.println("/docreatetask in here");
        model.addAttribute("task", task);
        System.out.println("+++++"+task.getDeadline());// deadline is null  
        return "createtask";
    }
        ...

Create.jsp

创建.jsp

...
<form:form method="POST"
action="${pageContext.request.contextPath}/docreatetask"
commandName="task">
<table class="formtable">
    <tr>
        <td class="label">Task</td>
        <td><form:input cssClass="control" path="taskname"
            name="taskname" type="text" /><br />
                <form:errors path="taskname" cssClass="error" /></td>
        </tr>
    <tr>
        <td class="label">Description</td>
        <td><form:textarea cssClass="control" path="description"
            name="description"></form:textarea><br />
                    <form:errors path="description" cssClass="error" /></td>
    </tr>
    <tr>
        <td class="label">Deadline (dd/mm/yyyy)</td>
        <td><form:input cssClass="control" path="deadline"
            name="deadline" type="date" /><br />
                <form:errors path="deadline" cssClass="error"></form:errors></td>
    </tr>
        ...

In the controller I wrote the following with the same error (and different formats like "yyyy/MM/dd")

在控制器中,我写了以下相同的错误(以及不同的格式,如“yyyy/MM/dd”)

@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.setLenient(false);
    webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

I also tried to add annotation in class (as well with different formats) but same error

我还尝试在类中添加注释(以及不同的格式)但同样的错误

?    ...
    @Column(name = "deadline")
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date deadline;
   ...

回答by borjab

Add this in the controller. Change the dateFormat to your locale preference.

在控制器中添加这个。将 dateFormat 更改为您的区域设置首选项。

@InitBinder     
public void initBinder(WebDataBinder binder){
     binder.registerCustomEditor(       Date.class,     
                         new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true, 10));   
}

Consider using this annotations in your model (Adapting format and TemporalType for your preferences)

考虑在您的模型中使用此注释(根据您的喜好调整格式和 TemporalType)

@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.DATE)

回答by xXxlazharxXx

simply add @DateTimeFormat(pattern = "yyyy-MM-dd") with (-)

只需添加 @DateTimeFormat(pattern = "yyyy-MM-dd") 和 (-)

example:

例子:

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateNaissance;

回答by WLiu

You need to change in BaseController for initBinder

您需要在 BaseController 中更改 initBinder

@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, null,  new CustomDateEditor(dateFormat, true));
}