title: Spring MVC - parse date and show binding errors
body: <p>In a Spring MVC controller, I want to parse a <code>Date</code> from a request using <code>@DateTimeFormat</code> and, in the case of a parsing error, I want to display an error message in my view indicating that the date is invalid.</p>
  
  <p>Here is my controller method:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST)
  public String form(
      @ModelAttribute 
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date, 
      BindingResult result) {
      if (result.hasErrors()) {
          System.out.println(result.toString()); // (1)
      }
      System.out.println(date); // (2)
  
      return "home";
  }
  </code></pre>
  
  <p>What I <strong>expect</strong>:</p>
  
  <p>"Happy case" (for <code>date=1999-10-24</code>): I expect <code>(2)</code> to print the date. </p>
  
  <p>"Error case" (unparseable date): I expect <code>(1)</code> to print details about the parsing failure.</p>
  
  <p>What is <strong>actually happening</strong>:</p>
  
  <p>"Happy case" (for <code>date=1999-10-24</code>): <code>(1)</code> prints a rejected value and <code>(2)</code> prints the correctly parsed date:</p>
  
  <pre><code>org.springframework.validation.BeanPropertyBindingResult: 1 errors
  Field error in object 'date' on field 'date': rejected value [1999-10-24]; ...
  Sun Oct 24 00:00:00 CEST 1999
  </code></pre>
  
  <p>"Error case": I see a <code>400 Bad Request</code> in the browser</p>
  
  <pre><code>Wed Oct 28 20:57:54 CET 2015
  There was an unexpected error (type=Bad Request, status=400).
  Failed to convert value of type 'java.lang.String' to required type 'java.util.Date';
  </code></pre>
  
  <p>I can reproduce this with Spring Boot 1.2.7.RELEASE.</p>
  
  <p>Link to example: <a href="https://github.com/shaehnel/spring-mvc-date" rel="nofollow">https://github.com/shaehnel/spring-mvc-date</a></p>
  
  <p>What am I missing? Thanks!</p>