gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:53:32 28 4
gpt4 key购买 nike

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

任务 Controller

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

在 Controller 中,我写了以下相同的错误(以及不同的格式,如“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;
...

最佳答案

只需将@DateTimeFormat(pattern = "yyyy-MM-dd") 与 (-) 相加

示例:

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

关于java - 无法将 java.lang.String 类型的属性值转换为所需的 java.util.Date 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23702041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com