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

问题描述 投票:0回答:6

当我尝试在表单中输入日期时收到此错误。 enter image description here

任务控制器

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

创建.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>
        ...

在控制器中,我编写了以下内容,但出现相同的错误(以及不同的格式,如“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));
}

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

​    ...
    @Column(name = "deadline")
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date deadline;
   ...
java spring date data-binding date-format
6个回答
30
投票

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

示例:

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

20
投票

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

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

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

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

3
投票
@DateTimeFormat(pattern = "yyyy-MM-dd")

用此替换您的格式。


2
投票

你可以这样做:

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

0
投票

您需要在 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));
}

0
投票

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

谢谢,它对我有用!

© www.soinside.com 2019 - 2024. All rights reserved.