简体   繁体   中英

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

I'm getting this error when I try to input a date in a form.在此处输入图像描述

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

...
<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")

@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;
   ...

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

@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)

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

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

example:

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

用这个替换你的格式。

You can do this:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate myDate;

You need to change in BaseController for 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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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