Skip to content

Commit 9883cbc

Browse files
committed
Add default converters for java.time types
Resolves #4257
1 parent 30563cd commit 9883cbc

21 files changed

+582
-50
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727
import org.springframework.batch.core.configuration.BatchConfigurationException;
2828
import org.springframework.batch.core.configuration.JobRegistry;
2929
import org.springframework.batch.core.converter.DateToStringConverter;
30+
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
31+
import org.springframework.batch.core.converter.LocalDateToStringConverter;
32+
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
3033
import org.springframework.batch.core.converter.StringToDateConverter;
34+
import org.springframework.batch.core.converter.StringToLocalDateConverter;
35+
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
36+
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
3137
import org.springframework.batch.core.explore.JobExplorer;
3238
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
3339
import org.springframework.batch.core.launch.JobLauncher;
@@ -380,6 +386,12 @@ protected ConfigurableConversionService getConversionService() {
380386
DefaultConversionService conversionService = new DefaultConversionService();
381387
conversionService.addConverter(new DateToStringConverter());
382388
conversionService.addConverter(new StringToDateConverter());
389+
conversionService.addConverter(new LocalDateToStringConverter());
390+
conversionService.addConverter(new StringToLocalDateConverter());
391+
conversionService.addConverter(new LocalTimeToStringConverter());
392+
conversionService.addConverter(new StringToLocalTimeConverter());
393+
conversionService.addConverter(new LocalDateTimeToStringConverter());
394+
conversionService.addConverter(new StringToLocalDateTimeConverter());
383395
return conversionService;
384396
}
385397

spring-batch-core/src/main/java/org/springframework/batch/core/converter/AbstractDateTimeConverter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class AbstractDateTimeConverter {
2727

2828
protected DateTimeFormatter instantFormatter = DateTimeFormatter.ISO_INSTANT;
2929

30-
protected DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_INSTANT;
30+
protected DateTimeFormatter localDateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
31+
32+
protected DateTimeFormatter localTimeFormatter = DateTimeFormatter.ISO_LOCAL_TIME;
33+
34+
protected DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
3135

3236
}

spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
* <ul>
6060
* <li>{@link java.util.Date}: in the
6161
* {@link java.time.format.DateTimeFormatter#ISO_INSTANT} format</li>
62+
* <li>{@link java.time.LocalDate}: in the
63+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format</li>
64+
* <li>{@link java.time.LocalTime}: in the
65+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format</li>
66+
* <li>{@link java.time.LocalDateTime}: in the
67+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format</li>
6268
* </ul>
6369
*
6470
* @author Dave Syer
@@ -74,6 +80,12 @@ public DefaultJobParametersConverter() {
7480
DefaultConversionService conversionService = new DefaultConversionService();
7581
conversionService.addConverter(new DateToStringConverter());
7682
conversionService.addConverter(new StringToDateConverter());
83+
conversionService.addConverter(new LocalDateToStringConverter());
84+
conversionService.addConverter(new StringToLocalDateConverter());
85+
conversionService.addConverter(new LocalTimeToStringConverter());
86+
conversionService.addConverter(new StringToLocalTimeConverter());
87+
conversionService.addConverter(new LocalDateTimeToStringConverter());
88+
conversionService.addConverter(new StringToLocalDateTimeConverter());
7789
this.conversionService = conversionService;
7890
}
7991

spring-batch-core/src/main/java/org/springframework/batch/core/converter/JsonJobParametersConverter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
* <ul>
5454
* <li>{@link java.util.Date}: in the
5555
* {@link java.time.format.DateTimeFormatter#ISO_INSTANT} format</li>
56+
* <li>{@link java.time.LocalDate}: in the
57+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format</li>
58+
* <li>{@link java.time.LocalTime}: in the
59+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format</li>
60+
* <li>{@link java.time.LocalDateTime}: in the
61+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format</li>
5662
* </ul>
5763
*
5864
* @author Mahmoud Ben Hassine
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.converter;
17+
18+
import java.time.LocalDateTime;
19+
import java.time.format.DateTimeFormatter;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
23+
/**
24+
* {@link Converter} implementation from {@link LocalDateTime} to {@link String}.
25+
*
26+
* This converter formats dates according to the
27+
* {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} format.
28+
*
29+
* @author Mahmoud Ben Hassine
30+
* @since 5.0.1
31+
*/
32+
public class LocalDateTimeToStringConverter extends AbstractDateTimeConverter
33+
implements Converter<LocalDateTime, String> {
34+
35+
@Override
36+
public String convert(LocalDateTime source) {
37+
return source.format(super.localDateTimeFormatter);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.converter;
17+
18+
import java.time.LocalDate;
19+
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
/**
23+
* {@link Converter} implementation from {@link LocalDate} to {@link String}.
24+
*
25+
* This converter formats dates according to the
26+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format.
27+
*
28+
* @author Mahmoud Ben Hassine
29+
* @since 5.0.1
30+
*/
31+
public class LocalDateToStringConverter extends AbstractDateTimeConverter implements Converter<LocalDate, String> {
32+
33+
@Override
34+
public String convert(LocalDate source) {
35+
return source.format(super.localDateFormatter);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.converter;
17+
18+
import java.time.LocalTime;
19+
import java.time.format.DateTimeFormatter;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
23+
/**
24+
* {@link Converter} implementation from {@link LocalTime} to {@link String}.
25+
*
26+
* This converter formats times according to the {@link DateTimeFormatter#ISO_LOCAL_TIME}
27+
* format.
28+
*
29+
* @author Mahmoud Ben Hassine
30+
* @since 5.0.1
31+
*/
32+
public class LocalTimeToStringConverter extends AbstractDateTimeConverter implements Converter<LocalTime, String> {
33+
34+
@Override
35+
public String convert(LocalTime source) {
36+
return source.format(super.localTimeFormatter);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.converter;
17+
18+
import java.time.LocalDate;
19+
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
/**
23+
* {@link Converter} implementation from {@link String} to {@link LocalDate}.
24+
*
25+
* This converter expects strings in the
26+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format.
27+
*
28+
* @author Mahmoud Ben Hassine
29+
* @since 5.0.1
30+
*/
31+
public class StringToLocalDateConverter extends AbstractDateTimeConverter implements Converter<String, LocalDate> {
32+
33+
@Override
34+
public LocalDate convert(String source) {
35+
return LocalDate.parse(source, super.localDateFormatter);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.converter;
17+
18+
import java.time.LocalDateTime;
19+
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
/**
23+
* {@link Converter} implementation from {@link String} to {@link LocalDateTime}.
24+
*
25+
* This converter expects strings in the
26+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format.
27+
*
28+
* @author Mahmoud Ben Hassine
29+
* @since 5.0.1
30+
*/
31+
public class StringToLocalDateTimeConverter extends AbstractDateTimeConverter
32+
implements Converter<String, LocalDateTime> {
33+
34+
@Override
35+
public LocalDateTime convert(String source) {
36+
return LocalDateTime.parse(source, super.localDateTimeFormatter);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.converter;
17+
18+
import java.time.LocalTime;
19+
20+
import org.springframework.core.convert.converter.Converter;
21+
22+
/**
23+
* {@link Converter} implementation from {@link String} to {@link LocalTime}.
24+
*
25+
* This converter expects strings in the
26+
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format.
27+
*
28+
* @author Mahmoud Ben Hassine
29+
* @since 5.0.1
30+
*/
31+
public class StringToLocalTimeConverter extends AbstractDateTimeConverter implements Converter<String, LocalTime> {
32+
33+
@Override
34+
public LocalTime convert(String source) {
35+
return LocalTime.parse(source, super.localTimeFormatter);
36+
}
37+
38+
}

spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
import javax.sql.DataSource;
2323

2424
import org.springframework.batch.core.converter.DateToStringConverter;
25+
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
26+
import org.springframework.batch.core.converter.LocalDateToStringConverter;
27+
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
2528
import org.springframework.batch.core.converter.StringToDateConverter;
29+
import org.springframework.batch.core.converter.StringToLocalDateConverter;
30+
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
31+
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
2632
import org.springframework.batch.core.repository.ExecutionContextSerializer;
2733
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
2834
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
@@ -168,6 +174,12 @@ public void afterPropertiesSet() throws Exception {
168174
DefaultConversionService conversionService = new DefaultConversionService();
169175
conversionService.addConverter(new DateToStringConverter());
170176
conversionService.addConverter(new StringToDateConverter());
177+
conversionService.addConverter(new LocalDateToStringConverter());
178+
conversionService.addConverter(new StringToLocalDateConverter());
179+
conversionService.addConverter(new LocalTimeToStringConverter());
180+
conversionService.addConverter(new StringToLocalTimeConverter());
181+
conversionService.addConverter(new LocalDateTimeToStringConverter());
182+
conversionService.addConverter(new StringToLocalDateTimeConverter());
171183
this.conversionService = conversionService;
172184
}
173185

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@
3737
import org.springframework.batch.core.JobParameter;
3838
import org.springframework.batch.core.JobParameters;
3939
import org.springframework.batch.core.converter.DateToStringConverter;
40+
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
41+
import org.springframework.batch.core.converter.LocalDateToStringConverter;
42+
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
4043
import org.springframework.batch.core.converter.StringToDateConverter;
44+
import org.springframework.batch.core.converter.StringToLocalDateConverter;
45+
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
46+
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
4147
import org.springframework.beans.factory.InitializingBean;
4248
import org.springframework.core.convert.support.ConfigurableConversionService;
4349
import org.springframework.core.convert.support.DefaultConversionService;
@@ -113,6 +119,12 @@ public JdbcJobExecutionDao() {
113119
DefaultConversionService conversionService = new DefaultConversionService();
114120
conversionService.addConverter(new DateToStringConverter());
115121
conversionService.addConverter(new StringToDateConverter());
122+
conversionService.addConverter(new LocalDateToStringConverter());
123+
conversionService.addConverter(new StringToLocalDateConverter());
124+
conversionService.addConverter(new LocalTimeToStringConverter());
125+
conversionService.addConverter(new StringToLocalTimeConverter());
126+
conversionService.addConverter(new LocalDateTimeToStringConverter());
127+
conversionService.addConverter(new StringToLocalDateTimeConverter());
116128
this.conversionService = conversionService;
117129
}
118130

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727
import org.apache.commons.logging.LogFactory;
2828

2929
import org.springframework.batch.core.converter.DateToStringConverter;
30+
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
31+
import org.springframework.batch.core.converter.LocalDateToStringConverter;
32+
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
3033
import org.springframework.batch.core.converter.StringToDateConverter;
34+
import org.springframework.batch.core.converter.StringToLocalDateConverter;
35+
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
36+
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
3137
import org.springframework.batch.core.repository.ExecutionContextSerializer;
3238
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
3339
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
@@ -241,6 +247,12 @@ public void afterPropertiesSet() throws Exception {
241247
DefaultConversionService conversionService = new DefaultConversionService();
242248
conversionService.addConverter(new DateToStringConverter());
243249
conversionService.addConverter(new StringToDateConverter());
250+
conversionService.addConverter(new LocalDateToStringConverter());
251+
conversionService.addConverter(new StringToLocalDateConverter());
252+
conversionService.addConverter(new LocalTimeToStringConverter());
253+
conversionService.addConverter(new StringToLocalTimeConverter());
254+
conversionService.addConverter(new LocalDateTimeToStringConverter());
255+
conversionService.addConverter(new StringToLocalDateTimeConverter());
244256
this.conversionService = conversionService;
245257
}
246258

0 commit comments

Comments
 (0)