title: Spring mvc form:form type data can't cast from Localdate to Date
body: <p>I'm trying to create a simple "expense tracker" with spring mvc 5 and hibernate 5.</p>
  
  <p>When I post without errors, I redirect towards the view and everything it's all right, but When I post and there is an error, the rendering of the view in the GET method fails, because of this:</p>
  
  <p>InputTag - java.time.LocalDate cannot be cast to java.util.Date</p>
  
  <p>How can I fix it?</p>
  
  <p>I tried with converters, with initBinder override methods.. but nothing worked. </p>
  
  <p>The field in the entity is annotated with @DateTimeFormat(pattern = "yyyy-MM-dd")</p>
  
  <p>The field in mysql is DATETIME. 
  I've added an InitBinder to format the localDate in the controller.. 
  Plus I can't find a similar issue on stackoverflow.
  Deeply thank u!</p>
  
  <p>I'm including the controller code:</p>
  
  <pre><code>@Controller
  public class ExpensesController {
  
  @Autowired
  private ExpenseService expenseService;
  
  @Autowired
  private SecurityService securityService;
  
  @InitBinder
  public void initBinder(WebDataBinder binder) {
      binder.registerCustomEditor(LocalDate.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
  }
  
  @RequestMapping(value = "/expCounter", method = RequestMethod.GET)
  public String showExpensesCounter(Model model)
  {
      String userName = securityService.getLoggedUserUsername();
      List&lt;Expense&gt; expenses = expenseService.findAllByUser(userName);
      model.addAttribute("expenses", expenses);
  
      if (!model.containsAttribute("expForm")) {
          model.addAttribute("expForm", new Expense());
      }
      return "expenses-counter";
  }
  
  @RequestMapping(value = "/expCounter", method = RequestMethod.POST)
  public String addExpense(@Valid @ModelAttribute("expForm") Expense expense,
      BindingResult result,
      ModelMap model, RedirectAttributes redirectAttributes)
  {
      if(result.hasErrors())
      {
          redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.expForm", result);
          redirectAttributes.addFlashAttribute("expForm", expense);
          return "redirect:expCounter";
      }
  
      expenseService.save(expense);
      return "redirect:expCounter";
  }
  }
  </code></pre>
  
  <p>This is the expense entity code:</p>
  
  <pre><code>@Entity
  @Table(name = "expense")
  public class Expense implements Serializable
  {
      private static final long serialVersionUID = -7179454458042397155L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      private Long id;
  
      @ManyToOne(fetch = FetchType.LAZY, optional = false)
      @JoinColumn(name="user_id", nullable=false)
  //    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
      @OnDelete(action = OnDeleteAction.CASCADE)
      private User user;
  
      @NotNull
      @DecimalMax("1000000")
      @Column(name = "amount")
      private BigDecimal amount;
  
      @Size(max = 250)
      @Column(name = "description")
      private String description;
  
      @NotNull
      @PastOrPresent
      @DateTimeFormat(pattern = "yyyy-MM-dd")
      @Column(name = "on_date", columnDefinition = "DATETIME")
      private LocalDate spentOnDate;
  //Constructor, Getters and Setters omitted for brevity
  </code></pre>
  
  <p>And this is the form:</p>
  
  <pre><code> &lt;form:form method="POST" action="expCounter" modelAttribute="expForm"&gt;
  &lt;div class="form-row mx-5 my-4 align-items-center text-center"&gt;
      &lt;div class="form-group col-4 text-uppercase"&gt;
          &lt;label class="control-label" for="description"&gt;
              &lt;p&gt;&lt;spring:message code="expenses.counter.description"/&gt;&lt;/p&gt;
          &lt;/label&gt;
          &lt;form:input path="description" cssClass="form-control" /&gt;
          &lt;form:errors path="description" /&gt;
      &lt;/div&gt;
      &lt;div class="form-group col-2 text-uppercase ${hasErrors ? 'has-error' : ''}"&gt;
          &lt;label class="control-label" for="amount"&gt;
              &lt;p&gt;&lt;spring:message code="expenses.counter.amount"/&gt;&lt;/p&gt;
          &lt;/label&gt;
          &lt;form:input path="amount" cssClass="form-control" /&gt;
          &lt;form:errors path="amount" /&gt;
      &lt;/div&gt;
  
      &lt;!-- THIS IS THE DATE FIELD--&gt;
      &lt;div class="form-group col-2 text-uppercase"&gt;
          &lt;label class="control-label" for="spentOnDate"&gt;
              &lt;p&gt;&lt;spring:message code="expenses.counter.txdate"/&gt;&lt;/p&gt;
          &lt;/label&gt;
          &lt;spring:message code="expenses.placeholder.ondate" var="ondate.placeholder" /&gt;
          &lt;form:input type="date" path="spentOnDate" name="spentOnDate"
                      cssClass="form-control" placeholder='${ondate.placeholder}'/&gt;
          &lt;form:errors path="spentOnDate" /&gt;
      &lt;/div&gt;
  
  
      &lt;div class="form-group col-2"&gt;
          &lt;input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/&gt;
          &lt;form:button class="btn btn-secondary"&gt;
              &lt;spring:message code="general.button.submit"/&gt;
          &lt;/form:button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>I this it is a problem of DateFormatting.. because I specified the date format like yyyy-MM-dd  (because in mysql I save it like that..) but when the post fails and I get the view (with the data field filled) spring cannot cast the existing LocalDate(yyyy-MM-dd) to java.util.date... but I don't know how to fix it.</p>
  
  <p>I found a workaround: simply changed input tag from spring's form:form to:</p>
  
  <pre><code>&lt;input type="date" class="form-control" name="spentOnDate" placeholder="${ondate.placeholder}"/&gt;
  </code></pre>
  
  <p>It now works but would be a lot better to be able to use spring's form:form tag. Do you know how to fix it?
  This way I could use the formatter on the FE.</p>
  
  <pre><code>&lt;fmt:formatDate value="${expForm.spentOnDate}" var="installDate" pattern="dd/mm/yyyy" /&gt;
  </code></pre>