Main Tutorials

Spring inject Date into bean property – CustomDateEditor

Spring example to show you how to inject a “Date” into bean property.


package com.mkyong.common;

import java.util.Date;

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 + "]";
	}

}

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="customer" class="com.mkyong.common.Customer">
		<property name="date" value="2010-01-31" />
	</bean>

</beans>

Run it


package com.mkyong.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);
		
	}
}

You will encounter follow error messages :


Caused by: org.springframework.beans.TypeMismatchException: 
	Failed to convert property value of type [java.lang.String] to 
	required type [java.util.Date] for property 'date'; 

nested exception is java.lang.IllegalArgumentException: 
	Cannot convert value of type [java.lang.String] to
	required type [java.util.Date] for property 'date': 
	no matching editors or conversion strategy found

Solution

In Spring, you can inject a Date via two methods :

1. Factory bean

Declare a dateFormat bean, in “customer” bean, reference “dateFormat” bean as a factory bean. The factory method will call SimpleDateFormat.parse() to convert String into Date object automatically.


<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.mkyong.common.Customer">
		<property name="date">
			<bean factory-bean="dateFormat" factory-method="parse">
				<constructor-arg value="2010-01-31" />
			</bean>
		</property>
	</bean>

</beans>

2. CustomDateEditor

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>

And declares another “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>

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.mkyong.common.Customer">
		<property name="date" value="2010-02-31" />
	</bean>

</beans>

Download Source Code

Download It – Spring-CustomDateEditor-Example.zip (5KB)

Reference

  1. Spring property editors

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Shwetha
3 years ago

How we need to do for localDate in spring bean?

Younis Irshad
8 years ago

In CUSTOMER CLASSS Use to get : Customer [date=Thu Aug 13 11:42:06 EDT 2015]

Date date = new Date();

SPRINGSBEANS.XML:

YOU WILL GET TODAY’S DATE WITH EXACT TIME

javacrazy
8 years ago

Hi Make The Date Change.Feb Should not be 31.

Prasad
9 years ago

Which jar we can find the “com.springframework.beans.factory.config.CustomerEditorConfigurer” in spring?

Drew
9 years ago

I noticed that in Spring 4.0.X, the CustomDateEditor approach has changed. Here’s a great article that helped me a lot: http://stackoverflow.com/questions/18706869/string-to-date-conversion-in-spring-4-0

Jake
10 years ago

In any programming, how often is hard coded date used?
What or how could the date be handled if it changes all the time in Spring?

Jeff
10 years ago

Thanks for the examples, it worked.
But on using method 2: CustomDateEditor, getting below Warning, any insights on how to overcome this.

I am using Spring 3.2.4

WARNING: Passing PropertyEditor instances into CustomEditorConfigurer is deprecated: use PropertyEditorRegistrars or PropertyEditor class names instead. Offending key [java.util.Date; offending editor instance: org.springframework.beans.propertyeditors.CustomDateEditor@1a729c1

Tushar
11 years ago

Cool 🙂
MkYong 🙂

Thanks for the Example:)
it really helped me a lot 🙂

mwmahlberg
11 years ago

Awesome!

The first solution is propably what almost everybody wants instead of re-implementing it.

Thanks a lot!

Pranav
11 years ago

I am getting “2010-03-03” as output for injected date “2010-02-31” . Why ??

Pranav
11 years ago
Reply to  Pranav

Ohhhh!!! Got it… you have used “2010-02-31″… 31st Feb,2010… Which is never possible in calendar.
@mkyoung: You couldn’t find any other date for example. 😀 🙂

Balamurugan
11 years ago

Sometimes I feel like, it is a magic in Spring.

Nice way of putting across to everyone.

Still I am not able to convince myself on the above program.
I am not able to see the logic behind in Spring.

Vinutha
11 years ago

Simply Superb mkyong!!! Hats off

Jeet
11 years ago

Hi Yong.

Thanks a lot its really helpful, brilliant !!!

Ravi
11 years ago

I love you man..!!!
Searched the entire web, but you have the solution of what I wanted.

John Guzman
12 years ago

Thanks! Well done! 😉

caroline
12 years ago

there is a error?theme word is error