Results 1 to 9 of 9

Thread: set a date value in a bean property

  1. #1
    Join Date
    Oct 2007
    Posts
    142

    Default set a date value in a bean property

    Hi

    How to set a date value on bean ?

    <bean id="beanid" class="BeanClass">
    <property name="date" value="2008-03-01"/>
    </bean>

  2. #2
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    By default there isn't a date PropertyEditor registered. PropertyEditors are the objects which convert strings into other object types. There are many PropertyEditors registered by default. Things like numbers, Class, etc. So you need to create a CustomDateEditor and then register it with your ApplicationContext using CustomEditorConfigurer. Take a look here for more information.
    Bill

  3. #3
    Join Date
    Oct 2007
    Location
    Cleveland, OH
    Posts
    3

    Thumbs up Quick Configuration for registering CustomDateEditor

    Just add the below configuration right after your <beans> definition.
    This would take care of registering the CustomDateEditor with the SHORTdate format (mm/dd/yy).
    PHP Code:
    <!-- PATTERNRegister the CustomDateEditor with SHORT date format (3) -->    
        <
    bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
            <
    property name="customEditors">
                <
    map>
                    <
    entry key="java.util.Date">                
                        <
    bean id="date_editor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
                            <
    constructor-arg>
                                <
    bean class="java.text.DateFormat" factory-method="getDateInstance">
                                    <
    constructor-arg value="3"/>
                                </
    bean>
                            </
    constructor-arg>
                            <
    constructor-arg value="true"/>
                        </
    bean>
                    </
    entry>
                </
    map>
            </
    property>
        </
    bean>    
    <!-- 
    End of Custom Date Editor Registering! --> 

    With this in place, you can now configure String-values for Date (java.util.Date) properties in your Bean definition file:
    PHP Code:
    <bean id="id" class="com.som.Identity">
        <
    property name="issueDate" value="1/1/2008" />
    </
    bean
    The Bean (for your refernence) would look like this:
    PHP Code:
    Identity.java

    public class Identity {
        public 
    Identity() { }

        
    // Inject
        
    java.util.Date issueDate;
        public 
    void setIssueDate(java.util.Date issueDate) {
            
    this.issueDate issueDate;
        }
        public 
    java.util.Date getIssueDate() {
            return 
    issueDate;
        }

    Let me know if there is any better way to do this.
    The concern that I have with this approach is using hardcoded '3' for SHORT in the Bean configuration. This doesn't seem great idea. (What if Sun decides to change this value to something else?).
    Som
    ----------
    Java Admirer!

  4. #4
    Join Date
    Oct 2007
    Posts
    142

    Default What abount using factory method

    PHP Code:
    <bean id="id" class="com.som.Identity"
        <
    property name="issueDate">
            <
    bean class="java.util.GregorianCalendar" factory-method="getTime"
                <
    constructor-arg value="2008"/>
                <
    constructor-arg value="1"/>
                <
    constructor-arg value="1"/>
            </
    bean
        </
    property>
    </
    bean

  5. #5
    Join Date
    Oct 2007
    Posts
    142

    Default It does not work

    maby factory method should be static

  6. #6
    Join Date
    Sep 2007
    Location
    Paris
    Posts
    37

    Default Inject bean getter return value ?

    Hi,

    I'm facing the exact same problem.
    GregorianCalendar.getTime() is not static, it's a regular getter.

    How to reference not GregorianCalendar bean but it's getTime() method return value ?

    this doesn't work
    PHP Code:
    <bean id="initDay" class="java.util.GregorianCalendar">
        <
    constructor-arg value="2008" />
        <
    constructor-arg value="0" />
        <
    constructor-arg value="1" />
    </
    bean>
    <
    bean id="id" class="com.som.Identity">
        <
    property name="issueDate" ref="initDay.time"/>
    </
    bean

  7. #7
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Reference documentation - 3.2.3.2.3. Instantiation using an instance factory method

  8. #8
    Join Date
    Sep 2007
    Location
    Paris
    Posts
    37

    Thumbs up Thank you

    It works !
    I had missed this in the doc. I was stupidly associating "factory" word with "static" method (wich is really stupid), so I jumped over this paragraph

    final conf:
    PHP Code:
    <bean id="initDay" class="java.util.GregorianCalendar">
        <
    constructor-arg value="2008" />
        <
    constructor-arg value="0" />
        <
    constructor-arg value="1" />
    </
    bean>
    <
    bean id="id" class="com.som.Identity">
        <
    property name="issueDate"><bean factory-bean="initDay" factory-method="getTime"/></property>
    </
    bean
    Thank you Denis

  9. #9
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Don't blame yourself, everyone makes a mistakes

    This forum is intended to be a place where people can help to each other. Believe me, it was not hard for me to point to the necessary documentation location

Posting Permissions

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