一、日期格式化配置DateFormatConfig
@Configuration
public class DateFormatConfig {
@Bean
@Primary
public ObjectMapper serializingObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setTimeZone(TimeZone.getDefault());
objectMapper.setDateFormat(new SimpleDateFormat(DateFormatUtil.PATTERN_DEFAULT_ON_SECOND));
objectMapper.registerModule(javaTimeModule);
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
simpleModule.addSerializer(Date.class, new DateSerializer());
simpleModule.addDeserializer(Date.class, new DateDeserializer());
objectMapper.registerModule(simpleModule);
return objectMapper;
}
/**
* @author xiutu
* @description Date 时间类型装换
*/
@Component
public class DateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException, IOException {
try {
String formattedDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date);
String dateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateFormatUtil.pareDate(formattedDate));
gen.writeString(dateStr);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
/**
* @author xiutu
* @description Date 时间类型装换
*/
@Component
public class DateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
try {
String param = jsonParser.getValueAsString();
Date date = DateFormatUtil.pareDate(param);
return date;
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
/**
* @author xiutu
* @description LocalDate 时间类型装换
*/
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
Date ldate = Date.from(value.atZone(ZoneId.systemDefault()).toInstant());
Date date = null;
try {
date = DateFormatUtil.pareDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, ldate));
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), TimeZone.getDefault().toZoneId());
gen.writeString(localDateTime.format(DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS)));
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
/**
* @author xiutu
* @description LocalDate 时间类型装换
*/
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
try {
String param = p.getValueAsString();
Date date = DateFormatUtil.pareDate(param);
String str = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,date);
LocalDateTime dateTime = LocalDateTime.parse(str, DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS));
return dateTime;
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
二、建立日期格式化工具DateFormatUtil
public class DateFormatUtil {
public static final String SYMBOL_DOT = "\\.";
public static final String DATE_REGEX_YYYYMM = "^\\d{4}-\\d{1,2}$";//日期正则yyyy-MM
public static final String DATE_REGEX_YYYYMMDD = "^\\d{4}-\\d{1,2}-\\d{1,2}$";//日期正则yyyy-MM-dd
public static final String DATE_REGEX_YYYYMMDDHHMM = "^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$";//日期正则yyyy-MM-dd hh:mm
public static final String DATE_REGEX_YYYYMMDDHHMMSS = "^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$";//日期正则yyyy-MM-dd hh:mm:ss
public static final String DATE_REGEX_YYYYMMDD_HHMMSS_SSS = "^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}\\.\\d{1,3}$";//日期正则yyyy-MM-dd HH:mm:ss.SSS
public static final String DATE_REGEX_SECOND_DOT_NANOSECOND = "^[0-9]{1,}\\.[0-9]{1,9}$";//Instant日期秒+纳秒
public static final String DATE_REGEX_YYYYMMDD_T_HHMMSS_Z = "^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{1,2}:\\d{1,2}Z$";//日期正则yyyy-MM-dd'T'HH:mm:ssZ
public static final String DATE_REGEX_YYYYMMDD_T_HHMMSS = "^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{1,2}:\\d{1,2}$";//日期正则yyyy-MM-dd'T'HH:mm:ss
public static final String DATE_REGEX_YYYYMMDD_T_HHMMSS_SSS_Z = "^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{1,2}:\\d{1,2}\\.\\d{1,3}Z$";//日期正则yyyy-MM-dd'T'HH:mm:ss.SSSZ
public static final String DATE_REGEX_YYYYMMDD_T_HHMMSS_SSS = "^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{1,2}:\\d{1,2}\\.\\d{1,3}$";//日期正则yyyy-MM-dd'T'HH:mm:ss.SSS
// 以T分隔日期和时间,并带时区信息,符合ISO8601规范
public static final String PATTERN_ISO = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";
public static final String PATTERN_ISO_ON_ONE = "yyyy-MM-dd'T'HH:mm:ss.SSS";
public static final String PATTERN_ISO_ON_SECOND = "yyyy-MM-dd'T'HH:mm:ssZZ";
public static final String PATTERN_ISO_ON_THREE = "yyyy-MM-dd'T'HH:mm:ss";
public static final String PATTERN_ISO_ON_DATE = "yyyy-MM-dd";
public static final String PATTERN_ISO_ON_MONTH = "yyyy-MM";
// 以空格分隔日期和时间,不带时区信息
public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String PATTERN_DEFAULT_ON_SECOND = "yyyy-MM-dd HH:mm:ss";
public static final String PATTERN_DEFAULT_ON_MINUTE = "yyyy-MM-dd HH:mm";
// 使用工厂方法FastDateFormat.getInstance(), 从缓存中获取实例
// 以T分隔日期和时间,并带时区信息,符合ISO8601规范
public static final FastDateFormat ISO_FORMAT = FastDateFormat.getInstance(PATTERN_ISO);
public static final FastDateFormat ISO_FORMAT_ON_ONE = FastDateFormat.getInstance(PATTERN_ISO_ON_ONE);
public static final FastDateFormat ISO_ON_SECOND_FORMAT = FastDateFormat.getInstance(PATTERN_ISO_ON_SECOND);
public static final FastDateFormat ISO_ON_THREE_FORMAT = FastDateFormat.getInstance(PATTERN_ISO_ON_THREE);
public static final FastDateFormat ISO_ON_DATE_FORMAT = FastDateFormat.getInstance(PATTERN_ISO_ON_DATE);
public static final FastDateFormat ISO_ON_MONTH_FORMAT = FastDateFormat.getInstance(PATTERN_ISO_ON_MONTH);
// 以空格分隔日期和时间,不带时区信息
public static final FastDateFormat DEFAULT_FORMAT = FastDateFormat.getInstance(PATTERN_DEFAULT);
public static final FastDateFormat DEFAULT_ON_SECOND_FORMAT = FastDateFormat.getInstance(PATTERN_DEFAULT_ON_SECOND);
public static final FastDateFormat DEFAULT_ON_MINUTE_FORMAT = FastDateFormat.getInstance(PATTERN_DEFAULT_ON_MINUTE);
/**
* 将日期格式的字符串转换成指定格式的日期
* @param pattern 日期格式
* @param dateString 日期字符串
* @return
* @throws ParseException
*/
public static Date pareDate(@NotNull String pattern, @NotNull String dateString) throws ParseException, ParseException {
return FastDateFormat.getInstance(pattern).parse(dateString);
}
/**
* 将日期格式的字符串根据正则转换成相应格式的日期
* @param dateString 日期字符串
* @return
* @throws ParseException
*/
public static Date pareDate(@NotNull String dateString) throws ParseException {
String source = dateString.trim();
if (StringUtils.isNotBlank(source)) {
if(source.matches(DATE_REGEX_YYYYMM)){
return ISO_ON_MONTH_FORMAT.parse(source);
} else if(source.matches(DATE_REGEX_YYYYMMDD)){
return ISO_ON_DATE_FORMAT.parse(source);
}else if(source.matches(DATE_REGEX_YYYYMMDDHHMM)){
return DEFAULT_ON_MINUTE_FORMAT.parse(source);
}else if(source.matches(DATE_REGEX_YYYYMMDDHHMMSS)){
return DEFAULT_ON_SECOND_FORMAT.parse(source);
}else if(source.matches(DATE_REGEX_YYYYMMDD_T_HHMMSS_Z)){
return ISO_ON_SECOND_FORMAT.parse(source);
} else if (source.matches(DATE_REGEX_YYYYMMDD_T_HHMMSS)) {
return ISO_ON_THREE_FORMAT.parse(source);
} else if(source.matches(DATE_REGEX_YYYYMMDD_T_HHMMSS_SSS_Z)){
return ISO_FORMAT.parse(source);
} else if (source.matches(DATE_REGEX_YYYYMMDD_T_HHMMSS_SSS)) {
return ISO_FORMAT_ON_ONE.parse(source);
} else if(source.matches(DATE_REGEX_SECOND_DOT_NANOSECOND)){
String[] split = source.split(SYMBOL_DOT);
return Date.from(Instant.ofEpochSecond(Long.parseLong(split[0]), Long.parseLong(split[1])));
} else if (source.matches(DATE_REGEX_YYYYMMDD_HHMMSS_SSS)) {
return DEFAULT_FORMAT.parse(source);
}else {
throw new IllegalArgumentException("Invalid date value '" + source + "'");
}
}
return null;
}
}