title: Accepting DateTime (Jodatime) as request method argument in SpringMVC
body: <p>I have a model class</p>
  
  <pre class="lang-java prettyprint-override"><code>class SomeClass {
    private DateTime myDate;
    // setter &amp; getter
  }
  </code></pre>
  
  <p>and a controller:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  class MyController {
    @RequestMapping("...")
    public String doStuff(@ModelAttribute("myAttribute") SomeClass value) {
      // ...
    }
  }
  </code></pre>
  
  <p>When calling this controller from a HTML5 form with an appropriate <code>&lt;input type="datetime" .../&gt;</code> field I get the following error:</p>
  
  <blockquote>
    <p>Field error in object 'myAttribute' on field 'myDate':
    rejected value [2014-02-08T23:00:00.00Z]; codes
    [typeMismatch.myAttribute.myDate,typeMismatch.myDate,typeMismatch.org.joda.time.DateTime,typeMismatch];
    arguments
    [org.springframework.context.support.DefaultMessageSourceResolvable:
    codes [myAttribute.myDate,myDate]; arguments [];
    default message [myDate]]; default message [Failed to convert
    property value of type 'java.lang.String' to required type
    'org.joda.time.DateTime' for property 'myDate'; nested
    exception is java.lang.IllegalStateException: Cannot convert value of
    type [java.lang.String] to required type [org.joda.time.DateTime] for
    property 'myDate': <strong>no matching editors or conversion strategy
    found]</strong></p>
  </blockquote>
  
  <p>This confuses me because as I understand <code>7.6.5 Configuring Formatting in Spring MVC</code> in the <a href="http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html" rel="nofollow">manual</a>:</p>
  
  <blockquote>
    <p>Full support for the Joda Time formatting library is also installed if
    Joda Time is present on the classpath.</p>
  </blockquote>
  
  <p>The conversion should just work out of the box (I am using spring 4.0 and Joda time 2.3). At least it should find a converter even if the format may be wrong. However I thought that should try to configure the date format as explained in section <code>7.7 Configuring a global date &amp; time format</code>. With some additional help from google about the format of HTML5 date fields and Javadoc I came up with this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"&gt;
      &lt;property name="formatterRegistrars"&gt;
          &lt;set&gt;
            &lt;bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar"&gt;
                &lt;property name="useIsoFormat" value="true" /&gt;
            &lt;/bean&gt;
          &lt;/set&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>However the effect of those lines which I just inserted into my applicationContext.xml had no effect at all.</p>
  
  <p>So what do I need to do to set things up correctly (additionally I want to avoid the <code>@DateTimeFormat</code> annotation on my model object, because my model doesn't know anything about HTML so it feels wrong to me to attach the information about the format in HTML5 on that object).</p>