1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import cn.hutool.core.date.DatePattern; import io.github.vampireachao.stream.core.optional.Sf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.GenericConversionService;
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List;
@Configuration public class ConvertConfig {
@Autowired public <T, R> ConvertConfig(ConversionService conversionService, List<Converter<T, R>> converters) { GenericConversionService genericConversionService = (GenericConversionService) conversionService; genericConversionService.addConverter(new Converter<String, LocalDate>() { @Override public LocalDate convert(String source) { return Sf.ofStr(source).mayLet(text -> LocalDate.parse(text, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))).get(); } }); genericConversionService.addConverter(new Converter<String, LocalDateTime>() { @Override public LocalDateTime convert(String source) { return Sf.ofStr(source).mayLet(text -> LocalDateTime.parse(text, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))).get(); } }); converters.forEach(genericConversionService::addConverter); }
}
|