SrpingBoot 中jackson格式化LocalDate、Date日期类型

摘要:

  最近在学习Springboot框架,在用jackson格式化实体类中Date和LocalDate时,遇到了一系列问题,在此做下记录。

1.LocalDate

     从Java 8之后,Java.time包下,添加了日期处理类
     新增的日期jar主要有三种:
     java.time.LocalDate  ->只对年月日做出处理
     java.time.LocalTime  ->只对时分秒纳秒做出处理
     java.time.LocalDateTime ->同时可以处理年月日和时分秒

     但是为啥还需要搞一套全新的处理日期和时间的API?是因为旧的java.util.Date比较难用:

     1.java.util.Date月份从0开始,一月是0,十二月是11.java.time.LocalDate月份和星期都改成了enum,可以有效防止用错。

     2.java.util.Date和SimpleDateFormatter都不是线程安全的,而LocalDateLocalTime和最基本的String一样,是不变类型,不但线程安全,而且不能修改。

2.LocalDate用法

// 取当前日期:
LocalDate today = LocalDate.now(); // -> 2018-12-5
// 根据年月日取日期,12月就是12:
LocalDate crischristmas = LocalDate.of(2018, 12, 05); // -> 2014-12-25
// 根据字符串取:
LocalDate endOfFeb = LocalDate.parse("2018-12-05"); // 严格按照ISO yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式
LocalDate.parse("2018-12-05"); // 无效日期无法通过:DateTimeParseException: Invalid date

3. LocalDate日期转换

// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2018-12-01
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2018-12-02
// 取本月最后一天,再也不用计算是28,29,30还是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2018-12-31
// 取下一天:
LocalDate firstDayOf2019 = lastDayOfThisMonth.plusDays(1); // 变成了2019-01-01
// 取2019年1月第一个周一,这个计算用Calendar要死掉很多脑细胞:
LocalDate firstMondayOf2019 = LocalDate.parse("2019-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2019-01-07

4.LocalTime用法 

//LocalTime到毫秒位
LocalTime now = LocalTime.now();//11:25:50.095
//去掉毫秒
now.withNano(0); //11:25:50
//用构造方法创建LocalTime
LocalTime.of(10, 10, 20);
//字符串转换
LocalTime.parse("12:00:00");

 5.JDBC中数据库日期类型跟JAVA8的新类型关联起来

SQL -> Java
--------------------------
date -> LocalDate
time -> LocalTime
timestamp -> LocalDateTime

6.SpringBoot中Controller中日期类型(Date、LocalDate)数据绑定

     1.添加注解@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,如果用的是实体类,那么在实体类对应属性上添加该注解。该注解是用于将request中日期类型的参数按照pattern格式格式化,然后赋值给对应属性

7.SpringBoot中@ResponseBody注解Jackson 格式化输出日期

      Date类型的属性格式化有以下几种方法:

     1.在bean对象对应日期属性上添加@JsonFormat(pattern="yyyyMMdd")注解。

@JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")
private Date createTime;

      2.在apllication.yml加入下面配置就可以

#时间戳统一转换
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
  time-zone: GMT+8

     3. LocalDate、LocalDateTime、LocalTime类型数据 格式化:

      配置方案一:

             定义一个配置类,在其中定义两个Bean,即可完成全局日期格式化处理,同时还兼顾了Date和LocalDateTime并存。实体类中日期属性不需要添加注解,Date类型属性,会由application.yml中spring.jackson.date-format自动格式化,LocalDate类型的属性,则会被如下定义的LocalDateSerializer 格式化

扫描二维码关注公众号,回复: 4386605 查看本文章
@Configuration
public class LocalDateTimeSerializerConfig {

	@Value("${spring.jackson.date-format}")
    private String pattern ;

     // 方案一
    @Bean 
    public LocalDateSerializer localDateDeserializer() { 
        return new LocalDateSerializer(DateTimeFormatter.ofPattern(pattern)); 
    }
      
    @Bean 
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { 
    	return builder -> builder.serializerByType(LocalDate.class, localDateDeserializer()); 
    }

}

      实体类 :

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	
	private Long id;
	
	private String name;
	
	private Integer age;
	
	@DateTimeFormat(pattern = "yyyyMMdd")
	private LocalDate birthday;

}

     Controller:

	@GetMapping("/{id}")
	public User getUser(@PathVariable Long id){
		return userService.getUserById(id);
	}

配置方案二

有时候,我们对日期格式要做特殊的处理,全局的格式化方式无法满足我们需求是,使用该方案是非常好的选择,通过 @JsonFormat 注解我们可以更为精准的为日期字段格式化,它的优先级比方案一高,二者可结合使用

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	
	private Long id;
	
	private String name;
	
	private Integer age;
	
	@JsonFormat(pattern = "yyyyMMdd")
	private LocalDate birthday;

	public User(Long id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

}

配置方案三 

其实和第一种类似,只不过第一种的写法更加优雅简洁,如果有多种类型需要做统一格式化处理,这种方案也不是不可以考虑(经测试不能与方案二同时使用)

package com.carry.config;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

@Configuration
public class LocalDateTimeSerializerConfig {

    @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
    private String pattern;

    // 方案三
    @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.registerModule(javaTimeModule);
        return objectMapper;
    }

    public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
        @Override
        public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
                throws IOException {
            gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));
        }
    }

    public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext)
                throws IOException {
            return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern));
        }
    }
}

 推荐使用方案一与方案二

 本文好多内容是参考了一下两位大神的文章

参考资料地址:

https://www.liaoxuefeng.com/article/00141939241051502ada88137694b62bfe844cd79e12c32000

https://www.cnblogs.com/carrychan/p/9883172.html

猜你喜欢

转载自blog.csdn.net/u011544437/article/details/84823724