Spring Batch — convert Date to another format

Prateek
2 min readJul 17, 2024

--

In this example, we’ll read the date field from the csv file using Spring XML config and parse and convert it to another format

pom.xml — All the dependencies required for the applications

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>SpringExamples</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringExamples</name>
<url>http://maven.apache.org</url>

<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.37</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.37</version>
</dependency>

</dependencies>
</project>

Model

package com.mkyong.common;

public class Customer {

private String date;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

@Override
public String toString() {
return "Customer [date=" + date + "]";
}
}

PropertyEditorSupport.java

package com.mkyong.common;

import java.beans.PropertyEditorSupport;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class CustomLocalDateTimeEditor extends PropertyEditorSupport {

public CustomLocalDateTimeEditor() {
}

private String parseText(String text) {
String input = text.replace(" ", " ");
Locale locale = new Locale("en", "US");

DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("MMM d yyyy hh:mm:ss:SSSa")
.withLocale( locale );
LocalDateTime ldt = LocalDateTime.parse( input , formatter ) ;
LocalDate ld = ldt.toLocalDate();
String output = ld.format( DateTimeFormatter.BASIC_ISO_DATE ) ;
System.out.println( output ) ;

return output;
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(parseText(text));
}

@Override
public String getAsText() {
String value = parseText(String.valueOf(getValue()));
return (value != null ? value.toString() : "");
}
}

MainApp.java

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);
}
}

spring beans

<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 class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.lang.String" value="com.mkyong.common.CustomLocalDateTimeEditor" />
</map>
</property>
</bean>

<bean id="customer" class="com.mkyong.common.Customer">
<property name="date" value="Apr 1 2022 12:00:00:000AM" />
</bean>
</beans>

Output Logs —

20220401
Customer [date=20220401]

Reference —

https://mkyong.com/spring-batch/how-to-convert-date-in-beanwrapperfieldsetmapper/

--

--

Prateek
Prateek

Written by Prateek

Java Developer and enthusiast

No responses yet