Results 1 to 3 of 3

Thread: PropertyEditor for java.util.Calendar

  1. #1
    Join Date
    Oct 2005
    Location
    Bergen, Norway
    Posts
    128

    Default 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. #2
    Join Date
    Mar 2010
    Location
    Brussels
    Posts
    4

    Default 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>

  3. #3
    Join Date
    Oct 2005
    Location
    Bergen, Norway
    Posts
    128

    Default

    Quote 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •