The following sections shows how to fill data to java.util.Date type value.
Here is the Java bean class we are going to use.
package com.java2s.common; import java.util.Date; //from w w w . j a v a2 s . c om public class Customer { Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "Customer [date=" + date + "]"; } }
The following code shows how to parse a String value to a Date value and then set to Java bean.
It create a Java bean from java.text.SimpleDateFormat
and
parse in the format as yyyy-MM-dd
. Then it use the dateFormat bean as factory-bean
and use the parse
method as the factory-method
.
In the constructor-arg
tag it sets the value to the date properties.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="customer" class="com.java2s.common.Customer"> <property name="date"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="2010-01-31" /> </bean> </property> </bean> </beans>
In the second method to fill date value, we use the CustomDateEditor class.
In the bean xml configuration file it declares a CustomDateEditor class to convert String into java.util.Date.
<bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> </constructor-arg> <constructor-arg value="true" /> </bean>
Then it declares CustomEditorConfigurer to make Spring convert bean properties whose type is java.util.Date.
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <ref local="dateEditor" /> </entry> </map> </property> </bean>
Here is the full full example of bean configuration file.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> </constructor-arg> <constructor-arg value="true" /> </bean> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <ref local="dateEditor" /> </entry> </map> </property> </bean> <bean id="customer" class="com.java2s.common.Customer"> <property name="date" value="2010-02-31" /> </bean> </beans>
To run the application.
package com.java2s.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "SpringBeans.xml"); Customer cust = (Customer) context.getBean("customer"); System.out.println(cust); } }