这是在使用SpringMVC时经常会遇到的日期类型转换,直接上代码:
@Configuration
public class ConverterConfig {
private Logger logger = LoggerFactory.getLogger(ConverterConfig.class);
private String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
private String localDateFormat = "yyyy-MM-dd";
private String localTimeFormat = "HH:mm:ss";
private TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
private Locale locale = Locale.CHINA;
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
logger.info("日期与字符串互转:Locale:【{}】, TimeZone:【{}】, Date:【{}】, LocalDateTime:【{}】,LocalDate:【{}】,LocalTime:【{}】",
locale, timeZone.getDisplayName(), dateTimeFormat, dateTimeFormat, localDateFormat, localTimeFormat);
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormat);
final DateTimeFormatter localDateFormatter = DateTimeFormatter.ofPattern(localDateFormat);
final DateTimeFormatter localTimeFormatter = DateTimeFormatter.ofPattern(localTimeFormat);
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimeFormat);
return builder -> {
//地区、时区
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.locale(locale).timeZone(timeZone);
// 序列化
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter))
.serializerByType(LocalDate.class, new LocalDateSerializer(localDateFormatter))
.serializerByType(LocalTime.class, new LocalTimeSerializer(localTimeFormatter))
.serializerByType(Date.class, new DateSerializer(false, simpleDateFormat));
// 反序列化
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter))
.deserializerByType(LocalDate.class, new LocalDateDeserializer(localDateFormatter))
.deserializerByType(LocalTime.class, new LocalTimeDeserializer(localTimeFormatter))
.deserializerByType(Date.class,
new DateDeserializers.DateDeserializer(DateDeserializers.DateDeserializer.instance,
simpleDateFormat, dateTimeFormat));
};
}
/**
* LocalDate转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern(localDateFormat));
}
};
}
/**
* LocalDateTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(dateTimeFormat));
}
};
}
/**
* LocalTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalTime> localTimeConverter() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
return LocalTime.parse(source, DateTimeFormatter.ofPattern(localTimeFormat));
}
};
}
/**
* Date转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, Date> dateConverter() {
return new Converter<String, Date>() {
@Override
public Date convert(String source) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimeFormat);
try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
logger.error("Init Converter String to Date error!", e);
throw new RuntimeException(e);
}
}
};
}
}
欢迎关注我的公众号“平凡的程序员”。
标签:return,Converter,SpringMVC,public,DateTimeFormatter,LocalDateTime,new,序列化,class From: https://blog.51cto.com/qiguang/5878346