Hi
How to set a date value on bean ?
<bean id="beanid" class="BeanClass">
<property name="date" value="2008-03-01"/>
</bean>
Hi
How to set a date value on bean ?
<bean id="beanid" class="BeanClass">
<property name="date" value="2008-03-01"/>
</bean>
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
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:<!-- PATTERN: Register 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:
The Bean (for your refernence) would look like this:PHP Code:<bean id="id" class="com.som.Identity">
<property name="issueDate" value="1/1/2008" />
</bean>
Let me know if there is any better way to do 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;
}
}
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!
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>
maby factory method should be static
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>
Reference documentation - 3.2.3.2.3. Instantiation using an instance factory method
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:
Thank you DenisPHP 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>
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![]()