Add default converters for java.time types

Resolves #4257
This commit is contained in:
Mahmoud Ben Hassine
2023-02-15 23:50:45 +01:00
parent 30563cd25c
commit 9883cbc30e
21 changed files with 582 additions and 50 deletions

View File

@@ -27,7 +27,13 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.configuration.BatchConfigurationException;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.converter.DateToStringConverter;
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
import org.springframework.batch.core.converter.LocalDateToStringConverter;
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
import org.springframework.batch.core.converter.StringToDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
@@ -380,6 +386,12 @@ public class DefaultBatchConfiguration implements ApplicationContextAware {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new DateToStringConverter());
conversionService.addConverter(new StringToDateConverter());
conversionService.addConverter(new LocalDateToStringConverter());
conversionService.addConverter(new StringToLocalDateConverter());
conversionService.addConverter(new LocalTimeToStringConverter());
conversionService.addConverter(new StringToLocalTimeConverter());
conversionService.addConverter(new LocalDateTimeToStringConverter());
conversionService.addConverter(new StringToLocalDateTimeConverter());
return conversionService;
}

View File

@@ -27,6 +27,10 @@ class AbstractDateTimeConverter {
protected DateTimeFormatter instantFormatter = DateTimeFormatter.ISO_INSTANT;
protected DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_INSTANT;
protected DateTimeFormatter localDateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
protected DateTimeFormatter localTimeFormatter = DateTimeFormatter.ISO_LOCAL_TIME;
protected DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
}

View File

@@ -59,6 +59,12 @@ import org.springframework.util.StringUtils;
* <ul>
* <li>{@link java.util.Date}: in the
* {@link java.time.format.DateTimeFormatter#ISO_INSTANT} format</li>
* <li>{@link java.time.LocalDate}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format</li>
* <li>{@link java.time.LocalTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format</li>
* <li>{@link java.time.LocalDateTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format</li>
* </ul>
*
* @author Dave Syer
@@ -74,6 +80,12 @@ public class DefaultJobParametersConverter implements JobParametersConverter {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new DateToStringConverter());
conversionService.addConverter(new StringToDateConverter());
conversionService.addConverter(new LocalDateToStringConverter());
conversionService.addConverter(new StringToLocalDateConverter());
conversionService.addConverter(new LocalTimeToStringConverter());
conversionService.addConverter(new StringToLocalTimeConverter());
conversionService.addConverter(new LocalDateTimeToStringConverter());
conversionService.addConverter(new StringToLocalDateTimeConverter());
this.conversionService = conversionService;
}

View File

@@ -53,6 +53,12 @@ import org.springframework.batch.core.JobParameters;
* <ul>
* <li>{@link java.util.Date}: in the
* {@link java.time.format.DateTimeFormatter#ISO_INSTANT} format</li>
* <li>{@link java.time.LocalDate}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format</li>
* <li>{@link java.time.LocalTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format</li>
* <li>{@link java.time.LocalDateTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format</li>
* </ul>
*
* @author Mahmoud Ben Hassine

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.converter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.core.convert.converter.Converter;
/**
* {@link Converter} implementation from {@link LocalDateTime} to {@link String}.
*
* This converter formats dates according to the
* {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} format.
*
* @author Mahmoud Ben Hassine
* @since 5.0.1
*/
public class LocalDateTimeToStringConverter extends AbstractDateTimeConverter
implements Converter<LocalDateTime, String> {
@Override
public String convert(LocalDateTime source) {
return source.format(super.localDateTimeFormatter);
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.converter;
import java.time.LocalDate;
import org.springframework.core.convert.converter.Converter;
/**
* {@link Converter} implementation from {@link LocalDate} to {@link String}.
*
* This converter formats dates according to the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format.
*
* @author Mahmoud Ben Hassine
* @since 5.0.1
*/
public class LocalDateToStringConverter extends AbstractDateTimeConverter implements Converter<LocalDate, String> {
@Override
public String convert(LocalDate source) {
return source.format(super.localDateFormatter);
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.converter;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import org.springframework.core.convert.converter.Converter;
/**
* {@link Converter} implementation from {@link LocalTime} to {@link String}.
*
* This converter formats times according to the {@link DateTimeFormatter#ISO_LOCAL_TIME}
* format.
*
* @author Mahmoud Ben Hassine
* @since 5.0.1
*/
public class LocalTimeToStringConverter extends AbstractDateTimeConverter implements Converter<LocalTime, String> {
@Override
public String convert(LocalTime source) {
return source.format(super.localTimeFormatter);
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.converter;
import java.time.LocalDate;
import org.springframework.core.convert.converter.Converter;
/**
* {@link Converter} implementation from {@link String} to {@link LocalDate}.
*
* This converter expects strings in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format.
*
* @author Mahmoud Ben Hassine
* @since 5.0.1
*/
public class StringToLocalDateConverter extends AbstractDateTimeConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, super.localDateFormatter);
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.converter;
import java.time.LocalDateTime;
import org.springframework.core.convert.converter.Converter;
/**
* {@link Converter} implementation from {@link String} to {@link LocalDateTime}.
*
* This converter expects strings in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format.
*
* @author Mahmoud Ben Hassine
* @since 5.0.1
*/
public class StringToLocalDateTimeConverter extends AbstractDateTimeConverter
implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, super.localDateTimeFormatter);
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.converter;
import java.time.LocalTime;
import org.springframework.core.convert.converter.Converter;
/**
* {@link Converter} implementation from {@link String} to {@link LocalTime}.
*
* This converter expects strings in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format.
*
* @author Mahmoud Ben Hassine
* @since 5.0.1
*/
public class StringToLocalTimeConverter extends AbstractDateTimeConverter implements Converter<String, LocalTime> {
@Override
public LocalTime convert(String source) {
return LocalTime.parse(source, super.localTimeFormatter);
}
}

View File

@@ -22,7 +22,13 @@ import java.nio.charset.StandardCharsets;
import javax.sql.DataSource;
import org.springframework.batch.core.converter.DateToStringConverter;
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
import org.springframework.batch.core.converter.LocalDateToStringConverter;
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
import org.springframework.batch.core.converter.StringToDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
@@ -168,6 +174,12 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new DateToStringConverter());
conversionService.addConverter(new StringToDateConverter());
conversionService.addConverter(new LocalDateToStringConverter());
conversionService.addConverter(new StringToLocalDateConverter());
conversionService.addConverter(new LocalTimeToStringConverter());
conversionService.addConverter(new StringToLocalTimeConverter());
conversionService.addConverter(new LocalDateTimeToStringConverter());
conversionService.addConverter(new StringToLocalDateTimeConverter());
this.conversionService = conversionService;
}

View File

@@ -37,7 +37,13 @@ import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.converter.DateToStringConverter;
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
import org.springframework.batch.core.converter.LocalDateToStringConverter;
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
import org.springframework.batch.core.converter.StringToDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
@@ -113,6 +119,12 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new DateToStringConverter());
conversionService.addConverter(new StringToDateConverter());
conversionService.addConverter(new LocalDateToStringConverter());
conversionService.addConverter(new StringToLocalDateConverter());
conversionService.addConverter(new LocalTimeToStringConverter());
conversionService.addConverter(new StringToLocalTimeConverter());
conversionService.addConverter(new LocalDateTimeToStringConverter());
conversionService.addConverter(new StringToLocalDateTimeConverter());
this.conversionService = conversionService;
}

View File

@@ -27,7 +27,13 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.converter.DateToStringConverter;
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
import org.springframework.batch.core.converter.LocalDateToStringConverter;
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
import org.springframework.batch.core.converter.StringToDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateConverter;
import org.springframework.batch.core.converter.StringToLocalDateTimeConverter;
import org.springframework.batch.core.converter.StringToLocalTimeConverter;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
@@ -241,6 +247,12 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new DateToStringConverter());
conversionService.addConverter(new StringToDateConverter());
conversionService.addConverter(new LocalDateToStringConverter());
conversionService.addConverter(new StringToLocalDateConverter());
conversionService.addConverter(new LocalTimeToStringConverter());
conversionService.addConverter(new StringToLocalTimeConverter());
conversionService.addConverter(new LocalDateTimeToStringConverter());
conversionService.addConverter(new StringToLocalDateTimeConverter());
this.conversionService = conversionService;
}