全局时间格式化

简介: 需求经常会需要后端给前端传时间,有各种类型的时候,date、java8中LocalDateTime等等,虽然挺简单一个小事,但是也挺繁琐的,毕竟大家容易犯懒。

实践


代码(SpringBoot)


添加jackson依赖


  <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.2</version>
        </dependency>


修改application.properties


######################################
#jackson.date-format
######################################
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8


时间格式化配置类


package com.example.jsondemo;
import com.fasterxml.jackson.databind.SerializationFeature;
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.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
/**
 * @author CBeann
 * @create 2020-09-05 19:23
 */
@Configuration
@JsonComponent
public class DateFormatConfig {
    @Value("${spring.jackson.date-format}")
    private String pattern;
    /**
     * @description date 类型全局时间格式化
     * @date 2020-09-05 19:23
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() {
        return builder -> {
            TimeZone tz = TimeZone.getTimeZone("UTC");
            DateFormat df = new SimpleDateFormat(pattern);
            df.setTimeZone(tz);
            builder.failOnEmptyBeans(false)
                    .failOnUnknownProperties(false)
                    .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                    .dateFormat(df);
        };
    }
    /**
     * @description LocalDate 类型全局时间格式化
     * @date 2020-09-05 19:23
     */
    @Bean
    public LocalDateTimeSerializer localDateTimeDeserializer() {
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
    }
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
    }
}


实体类


@Data
public class Student {
    private String name;
    private Date date;
    private LocalDateTime localDateTime;
    private LocalDate localDate;
    private LocalTime localTime;
}


测试


@RequestMapping("/hello")
    public Object jsonObject() {
        Student student = new Student();
        student.setDate(new Date());
        student.setLocalDate(LocalDate.now());
        student.setLocalDateTime(LocalDateTime.now());
        student.setLocalTime(LocalTime.now());
        return student;
    }


测试结果


1.jpg


使用自定义格式


@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")


@Data
public class Student {
    private String name;
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")
    private Date date;
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")
    private LocalDateTime localDateTime;
    private LocalDate localDate;
    private LocalTime localTime;
}
目录
相关文章
|
2月前
|
Java
LocalDateTime的全局自定义序列化
LocalDateTime的全局自定义序列化
|
7月前
|
XML JSON 前端开发
SpingMVC中日期格式化与转换的那些事
SpingMVC中日期格式化与转换的那些事
59 0
|
2月前
时间类:定义对象并输出特定时间
时间类:定义对象并输出特定时间
14 0
|
8月前
|
JavaScript
vue获取系统默认的年月日时分秒
vue获取系统默认的年月日时分秒
91 1
|
8月前
|
JSON 前端开发 数据格式
全局日期请求转换处理
全局日期请求转换处理
51 0
|
9月前
|
前端开发 定位技术
前端将UTC时间格式转化为本地时间格式~~uniapp写法
前端将UTC时间格式转化为本地时间格式~~uniapp写法
75 0
|
11月前
|
JavaScript
js关于时间日期格式化方法
js关于时间日期格式化方法
159 0
|
JavaScript
JS之获取当前时间 增加 减少天数,并格式化返回(在Date原型上挂载方法实现)
JS之获取当前时间 增加 减少天数,并格式化返回(在Date原型上挂载方法实现)
187 0
JS之获取当前时间 增加 减少天数,并格式化返回(在Date原型上挂载方法实现)
|
JavaScript 数据库
时间日期格式化 moment库的基本使用
注意:在时间格式的传输过程中, 我们为了能使时间在每一个地区都能准确获取的,一般存入数据库的都是,utf8 或者 是时间戳的形式, 因为时间戳和utf8的是一个标准,不会因为地区而异而改变
时间日期格式化 moment库的基本使用