java8 LocalDate 类型 json 解析 日期格式处理

在使用java8的过程中用到了新的日期类LocalDate、LocalDateTime类型,作为属性不经过任何处理转成json的时候会变成下面的样式。

"applicationTime": {
        "month": "AUGUST",
        "year": 2017,
        "dayOfMonth": 2,
        "dayOfWeek": "WEDNESDAY",
        "dayOfYear": 214,
        "monthValue": 8,
        "hour": 16,
        "minute": 38,
        "second": 53,
        "nano": 0,
        "chronology": {
          "id": "ISO",
          "calendarType": "iso8601"
        }
      },

如何才能变成字符串的yyyy-MM-dd,需要导入几个类库

<properties>
    <jackson.version>2.8.5jackson.version>
properties>


        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>${jackson.version}version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>${jackson.version}version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>${jackson.version}version>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.datatypegroupId>
            <artifactId>jackson-datatype-jsr310artifactId>
            <version>${jackson.version}version>
        dependency>

只有jackson-datatype-jsr310是用来格式化日期用的

 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
 private LocalDateTime applicationTime;

或者

 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 private LocalDateTime applicationTime;

加上@JsonFormat注解,这样最后当对象转成json的时候就变成了

"applicationTime": "2017-08-02 16:38:59",

你可能感兴趣的:(json)