title: Spring: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime]
body: <p>I have a Spring Boot application running Spring MVC and Thymeleaf. One of its entities is the <code>Purchase</code> entity, containing the field <code>date</code> which is of <code>LocalDateTime</code> type:</p>
  
  <pre><code>@Entity
  public class Purchase {
  
      // Other fields
  
      @DateTimeFormat(pattern = "dd-MM-yyyy")
      private LocalDateTime date;
  
      // Getters and setters
  }
  </code></pre>
  
  <p>Please note that this field is already annotated with <code>@DateTimeFormat</code> specifying the <code>pattern</code> of <code>dd-MM-yyyy</code>, which happens to be the same one defined in my <code>form.html</code>:</p>
  
  <pre><code>&lt;form id="purchaseForm" th:action="@{/purchases/(form)}"
              th:object="${purchase}" action="#" method="post" class="form-horizontal"&gt;
  
      &lt;!-- Purchase's date --&gt;
      &lt;div class="form-group"&gt;  
          &lt;label class="control-label col-sm-2" for="text"&gt;Date&lt;/label&gt;
          &lt;input class="col-sm-10" type="text" id="date" th:field="*{date}" 
              th:class="${#fields.hasErrors('date')} ? 'field-error'"/&gt;
      &lt;/div&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>That given date is formatted in the form like <code>14-12-2017</code>. 
  This form is then processed by the <code>PurchaseController</code> after submission:</p>
  
  <pre><code>@Controller
  @RequestMapping("/purchases")
  public class PurchaseController {
  
      @PostMapping(params = "form")
      public ModelAndView create(@Valid Purchase purchase, BindingResult result,
              RedirectAttributes redirect) {
  
          // Do stuff
      }
  }
  </code></pre>
  
  <p>After submission, I get the following error:</p>
  
  <pre><code>Field error in object 'purchase' on field 'date': rejected value [12-12-2017]; 
  codes [typeMismatch.purchase.date,typeMismatch.date,typeMismatch.java.time.LocalDateTime,typeMismatch];
  arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 
  codes [purchase.date,date]; arguments []; default message [date]];
  default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'date';
  nested exception is org.springframework.core.convert.ConversionFailedException: 
  Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '12-12-2017'; 
  nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [12-12-2017]]
  </code></pre>
  
  <p>Which I don't quite understand the reason, since the rejected value <code>'12-12-2017'</code> follows the pattern I've defined within the <code>@DateTimeFormat</code> annotation. Moreover, the javadoc of <code>@DateTimeFormat</code> tells us that it works with the <code>java.time</code> new types.</p>
  
  <p>Any ideas? Thanks in advance.</p>