-
在 SpringWeb 中用 LocalDateTIme 作为接口的返回与参数时会有以下两个问题
- 接口返回日期格式中有一个
T
- 接口作为参数传入格式化会报错
@RestController
@RequestMapping("date")
public class LocalDateTimeController {
@Data
@Builder
@AllArgsConstructor
public static class LocalDateTimeForm {
private LocalDate date;
private LocalDateTime dateTime;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class LocalDateTimeVO {
private LocalDateTime localDateTime;
private LocalDateTimeForm form;
@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
private LocalDateTime jsonFormat;
private Date date;
}
@GetMapping
public LocalDateTimeVO parse(LocalDateTimeForm form) {
LocalDateTime now = LocalDateTime.now();
return LocalDateTimeVO.builder().localDateTime(now).jsonFormat(now).date(new Date()).form(form).build();
}
}
package com.framework.projectframework.configs;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
@Configuration
public class ChineseLocalDateTimeFormat {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String dateTimeFormat;
private static final String dateFormat = "yyyy-MM-dd";
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.simpleDateFormat(dateTimeFormat);
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
};
}
@Bean
public Formatter<LocalDate> localDateFormatter() {
return new Formatter<LocalDate>() {
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat);
@Override
public LocalDate parse(String text, Locale locale) throws ParseException {
return LocalDate.parse(text, dateTimeFormatter);
}
@Override
public String print(LocalDate localDate, Locale locale) {
return dateTimeFormatter.format(localDate);
}
};
}
@Bean
public Formatter<LocalDateTime> localDateTimeFormatter() {
return new Formatter<LocalDateTime>() {
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormat);
@Override
public LocalDateTime parse(String text, Locale locale) throws ParseException {
return LocalDateTime.parse(text, formatter);
}
@Override
public String print(LocalDateTime localDateTime, Locale locale) {
return formatter.format(localDateTime);
}
};
}
}
spring:
jackson:
locale: en_CH
time-zone: Asia/Shanghai
date-format: yyyy-MM-dd HH:mm:ss
-
这些都是我测试好实践的, 如果想详细研究一下可以看一下
- https://janus.blog.csdn.net/article/details/107065047
- https://www.baeldung.com/spring-boot-formatting-json-dates
- https://segmentfault.com/a/1190000022512201