From: https://blog.csdn.net/lianghecai52171314/article/details/127106664
方案一:给日期字段添加注解
/**
* 创建时间
*/
//返回时间类型
@JsonFormat(pattern = GlobalConst.DATETIME_PATTERN, timezone = "GMT+8")
//接收时间类型
@DateTimeFormat(pattern = GlobalConst.DATETIME_PATTERN)
private LocalDateTime createTime;
/**
* 更新时间
*/
//返回时间类型
@JsonFormat(pattern = GlobalConst.DATETIME_PATTERN, timezone = "GMT+8")
//接收时间类型
@DateTimeFormat(pattern = GlobalConst.DATETIME_PATTERN)
private LocalDateTime updateTime;
注意:
springboot默认依赖的json框架是jackson。当使用@ResponseBody注解返回json格式数据时就是该框架在起作用。@JsonFormat注解只有在使用@ResponseBody注解返回json格式数据时有效。
方案二
配置类
@Configuration
public class DateHandlerConfig {
/**
* 默认日期时间格式
*/
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 默认日期格式
*/
private static final String DATE_FORMAT = "yyyy-MM-dd";
/**
* 默认时间格式
*/
private static final String TIME_FORMAT = "HH:mm:ss";
/**
* Date转换器
*/
@Bean
public Converter<String, Date> dateConverter() {
return new Converter<String, Date>() {
@Override
public Date convert(String source) {
return DateUtil.parse(source.trim());
}
};
}
/**
* LocalDate转换器
*/
@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern(DATE_FORMAT));
}
};
}
/**
* LocalTime转换器
*/
@Bean
public Converter<String, LocalTime> localTimeConverter() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
return LocalTime.parse(source, DateTimeFormatter.ofPattern(TIME_FORMAT));
}
};
}
/**
* LocalDateTime转换器
*/
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
}
};
}
/**
* Json序列化和反序列化转换器
*/
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
//java8日期 Local系列序列化和反序列化模块
JavaTimeModule javaTimeModule = new JavaTimeModule();
//序列化
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
//反序列化
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
objectMapper.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(javaTimeModule);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
// 忽略json字符串中不识别的属性
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 忽略无法转换的对象
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// PrettyPrinter 格式化输出
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
// NULL不参与序列化
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 指定时区
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
/**
* Date日期类型字符串全局处理, 默认格式为:yyyy-MM-dd HH:mm:ss
* 局部处理某个Date属性字段接收或返回日期格式yyyy-MM-dd, 可采用@JsonFormat(pattern = "yyyy-MM-dd", timezone="GMT+8")注解标注该属性
*/
objectMapper.setDateFormat(new SimpleDateFormat(DATE_TIME_FORMAT));
return objectMapper;
}
}
上面配置类中用到的DateUtil工具类的代码如下:
public class DateUtil {
public static void main(String[] args) {
System.out.println(parse("2000-12"));
System.out.println(parse("2000-12-12"));
System.out.println(parse("2000-12-12 12:12"));
System.out.println(parse("2000-12-12 12:12:12"));
}
public static Date parse(String source) {
if (source == null || source.length() == 0) {
return null;
}
source = source.trim();
if (source.matches("^\\d{4}-\\d{1,2}$")) {
return parseDate(source, "yyyy-MM");
} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
return parseDate(source, "yyyy-MM-dd");
} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
return parseDate(source, "yyyy-MM-dd HH:mm");
} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
return parseDate(source, "yyyy-MM-dd HH:mm:ss");
} else {
throw new IllegalArgumentException("Invalid false value '" + source + "'");
}
}
/**
* 格式化日期
* @param dateStr String 字符型日期
* @param format String 格式
* @return Date 日期
*/
private static Date parseDate(String dateStr, String format) {
Date date;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(dateStr);
} catch (Exception e) {
throw new IllegalArgumentException(e.getLocalizedMessage());
}
return date;
}
}
标签:return,SpringBoot,FORMAT,source,日期,new,精品,objectMapper,String From: https://www.cnblogs.com/joeblackzqq/p/17716820.html