spring:
jackson:
# timeZone: GMT+8 無效參數
# dateFormat: yyyy-MM-dd HH:mm:ss 也是無效的
複製代碼
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.7</version>
</dependency>
複製代碼
@Configuration
public class LocalDateConfig {
/** * Date格式化字符串 */
private static final String DATE_FORMAT = "yyyy-MM-dd";
/** * DateTime格式化字符串 */
private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/** * Time格式化字符串 */
private static final String TIME_FORMAT = "HH:mm:ss";
/** * 添加自定義 * @return Jackson2ObjectMapperBuilderCustomizer */
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder
.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)))
.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
}
}
複製代碼
spring:
datasource:
druid:
url: jdbc:mysql://server.address:3306/ilss_blog?useSSL=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
複製代碼
你沒看錯,就是Asia/Shanghaijava
這個能夠填的值能夠在 /usr/share/zoneinfo 目錄下,可是你不能夠直接填Asia或者直接填Shanghai你須要填Asia/Shanghai 從zoneinfo的相對路徑開始填 mysql
是否是很簡單!你去試試吧。spring
感謝不知道看了哪些博客的主人。sql