24

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

...
<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;
   ...
4
  • Post the code of your JSP. Commented May 16, 2014 at 20:07
  • I think, I found out the error. I changed new SimpleDateFormat("dd/MM/yyyy") for new SimpleDateFormat("yyyy-MM-dd") Commented May 16, 2014 at 20:13
  • 1
    The pattern should be "yyyy-MM-dd". Careful with MM (months) and mm (minutes). Commented May 16, 2014 at 20:14
  • Yes, sorry by the misspelling Commented May 16, 2014 at 20:15

6 Answers 6

30

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

example:

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateNaissance;
Sign up to request clarification or add additional context in comments.

4 Comments

Well, I think this answer is unnecessary since is from 2 years ago.
but it helped me ;-)
what should i do in this case "2021-05-27 12:46:17"
You can also use: @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
20

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)

5 Comments

How does this associate with the pattern parameter of @DateTimeFormat if you need to provide your own DateFormat to the CustomDateEditor binding?
@ DateTimeFormat might be helpfull if you cast the field to string again. For example. (Convert to XML, Json, etc) or just getting the property in the JSP @ Temporal will control if you store the field as a DATE or DATETIME field in the database.
@borjab, thanks a lot! I totally forgot about initBinder... You saved me a lot of time!
How do I use the InitBinder to register a custom editor for OffsetDateTime.class?
You can also use: @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
3
@DateTimeFormat(pattern = "yyyy-MM-dd")

Replace your format with this.

Comments

2

You can do this:

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

Comments

0

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));
}

Comments

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

2 Comments

Thanks for wanting to contribute. I should say that this suggestion comes late, though. Today no one should use neither Date class. They were both badly designed and have both been obsolete since java.time, the modern Java date and time API, came out on Java 8, which was 11 years ago.
You're right Well, it's in case someone uses very old versions and doesn't pay much attention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.