Results 1 to 3 of 3

Thread: Date Property editor problem

  1. #1
    Join Date
    Jun 2012
    Posts
    16

    Default Date Property editor problem

    I am using the CustomDateEditor to convert the date from flat file. The flat file contains the date in ddmmyy format. The record is converted to Bean which is JPA entity.

    Code:
    <bean id="myFieldSetMapper"
    		class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
    		<property name="targetType" value="domain.MyClass" />
    		<property name="customEditors">
    			<map>
    				<entry key="org.springframework.batch.item.file.transform.Range[]">
    					<bean
    						class="org.springframework.batch.item.file.transform.RangeArrayPropertyEditor" />
    				</entry>
    				<entry key="java.util.Date">
    					<bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
    						<constructor-arg>
    							<bean class="java.text.SimpleDateFormat">
    								<constructor-arg value="ddMMyy" />
    							</bean>
    						</constructor-arg>
    						<constructor-arg value="false" />
    					</bean>
    				</entry>
    			</map>
    		</property>
    	</bean>
    I want to catch the Exception if the date is invalid in validation Processor.

    The codes are running fine. But when the invalid date occurs, it converted to some arbitrary date and no exception thrown. for example if the String passed to the date field is 351308 in ddmmyy format, it is converting to '04-feb-2009' instead of throwing the exception.

    Please suggest how to correct the problem. I am using spring 3.1.1 and spring batch 2.1.8.

    Thanks

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    This isn't a PropertyEditor problem that is default behavior of the SimpleDateFormat... By default it is lenient (i.e. allow illegal dates and convert them into something useable i.e. 31022012 would become 02032012)... Set lenient to false on the SimpleDateFormat and you will get an exception.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2012
    Posts
    16

    Default

    Thanks Marten. It is working.

Posting Permissions

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