spring boot 自定义配置文件的类型转换

String To Date 转换


public class StringToDateConverter implements Converter {

public Date convert(String source) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

try { return sdf.parse(source); }

catch (ParseException e) { e.printStackTrace(); return null; } } }

int To Date 转换

public class IntegerToDateConverter implements Converter {

public Date convert(Integer source) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

try { return sdf.parse(String.valueOf(source)); }

catch (ParseException e) { e.printStackTrace(); return null; } } }

注入类型转换bean

@Bean public ConversionService conversionService() {

ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();

Set converters = new HashSet<>();

converters.add(new StringToDateConverter());

converters.add(new IntegerToDateConverter());

bean.setConverters(converters);

bean.afterPropertiesSet();

return bean.getObject(); }

原文地址:https://blog.csdn.net/ACMer_AK/article/details/78705012

你可能感兴趣的:(spring boot 自定义配置文件的类型转换)