java date 序列化_序列化和反序列化Java 8的时间/日期类

序列化

假如有 Clock 类:

public class Clock {

private LocalDate localDate;

private LocalTime localTime;

private LocalDateTime localDateTime;

// getter and setter

}

默认情况下, jackson序列化结果为:

{

"localDate": {

"year": 2017,

"month": "FEBRUARY",

"chronology": {

"calendarType": "iso8601",

"id": "ISO"

},

"era": "CE",

"dayOfYear": 57,

"dayOfWeek": "SUNDAY",

"leapYear": false,

"dayOfMonth": 26,

"monthValue": 2

},

"localTime": {

"hour": 17,

"minute": 33,

"second": 53,

"nano": 56000000

},

"localDateTime": {

"hour": 17,

"minute": 33,

"second": 53,

"nano": 56000000,

"dayOfYear": 57,

"dayOfWeek": "SUNDAY",

"month": "FEBRUARY",

"dayOfMonth": 26,

"year": 2017,

"monthValue": 2,

"chronology": {

"calendarType": "iso8601",

"id": "ISO"

}

}

}

简直不能直视...

在添加 JSR-310 模块后, 让 jackson 能够识别出 Java 8 的日期 & 时间类型(pom.xml):

com.fasterxml.jackson.datatype

jackson-datatype-jsr310

然后再启用 ISO 8601格式化标准 后效果就好多了(application.properties):

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

格式化效果:

{

"localDate": "2017-02-26",

"localTime": "17:42:54.141",

"localDateTime": "2017-02-26T17:42:54.141"

}

然而这中样式不太符合日常使用习惯, 还是需要通过注解或自定义序列化类的方式实现定制. 这里采用注解的方式实现:

public class Clock {

private LocalDate localDate;

@JsonFormat(pattern = "HH:mm:ss")

private LocalTime localTime;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private LocalDateTime localDateTime;

// getter and setter

}

格式化效果:

{

"localDate": "2017-02-26",

"localTime": "17:45:06",

"localDateTime": "2017-02-26 17:45:06"

}

这样就完美多了~

反序列化

上面只是序列化配置, 如果想提交日期参数还是配置一下:

public class Clock {

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

private LocalDate localDate;

@JsonFormat(pattern = "HH:mm:ss")

@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)

private LocalTime localTime;

// @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private LocalDateTime localDateTime;

// getter and setter

}

DateTimeFormat.ISO.DATE_TIME 的格式是 yyyy-MM-dd'T'HH:mm:ss.SSSZ(e.g. 2000-10-31T01:30:00.000-05:00), 不太符合使用习惯, 因此需要自定义 pattern.

测试接口:

@RestController

@EnableAutoConfiguration

public class HomeController {

@RequestMapping("/clock")

public Object deserialization(Clock clock) {

if (null == clock.getLocalDate()) {

clock.setLocalDate(LocalDate.now());

}

if (null == clock.getLocalTime()) {

clock.setLocalTime(LocalTime.now());

}

if (null == clock.getLocalDateTime()) {

clock.setLocalDateTime(LocalDateTime.now());

}

return clock;

}

}

当请求 URL http://localhost:8080/clock?localDate=2017-01-01&localDateTime=2017-01-01%2001:01:01 时, 结果为:

{

"localDate": "2017-01-01",

"localTime": "17:53:34",

"localDateTime": "2017-01-01 01:01:01"

}

源码

参考链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值