Announcement Announcement Module
Collapse
No announcement yet.
PropertyEditor for java.util.Calendar Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyEditor for java.util.Calendar

    I cannot seem to find support for a PropertyEditor for java.util.Calendar? Haven't I looked thoroughly enough or is it missing?

  • #2
    CustomDateEditor class

    Calendar.getInstance().getTime() returns Date object

    then this is howu use CustomDateEditor class for convertign date strings into java.util.Date properties

    First, declare an instance of it in the bean configuration file.

    <beans ...>
    ...
    <bean id="dateEditor"
    class="org.springframework.beans.propertyeditors.C ustomDateEditor">
    <constructor-arg>
    <bean class="java.text.SimpleDateFormat">
    <constructor-arg value="yyyy-MM-dd" />
    </bean>
    </constructor-arg>
    <constructor-arg value="true" /> // indicates this editor allows empty values.
    </bean>
    </beans>

    Then register this property editor in a CustomEditorConfigurer instance


    <beans ...>
    ...
    <bean class="org.springframework.beans.factory.config.Cu stomEditorConfigurer">
    <property name="customEditors">
    <map>
    <entry key="java.util.Date">
    <ref local="dateEditor" />
    </entry>
    </map>
    </property>
    </bean>

    Comment


    • #3
      Originally posted by vishal.layka View Post
      Calendar.getInstance().getTime() returns Date object

      then this is howu use CustomDateEditor class for convertign date strings into java.util.Date properties

      First, declare an instance of it in the bean configuration file.

      <beans ...>
      ...
      <bean id="dateEditor"
      class="org.springframework.beans.propertyeditors.C ustomDateEditor">
      <constructor-arg>
      <bean class="java.text.SimpleDateFormat">
      <constructor-arg value="yyyy-MM-dd" />
      </bean>
      </constructor-arg>
      <constructor-arg value="true" /> // indicates this editor allows empty values.
      </bean>
      </beans>

      Then register this property editor in a CustomEditorConfigurer instance


      <beans ...>
      ...
      <bean class="org.springframework.beans.factory.config.Cu stomEditorConfigurer">
      <property name="customEditors">
      <map>
      <entry key="java.util.Date">
      <ref local="dateEditor" />
      </entry>
      </map>
      </property>
      </bean>
      Sure - this will work for string to date, but not string to Calendar - which was my question - so to make the story short - this is not supported by the framework - but no harder than that you extend the existing class, and override the toObject method to return a calendar instead.

      Comment

      Working...
      X