当前位置:网站首页>创建格式化时间,格式化时区的gson对象。json解析时间格式化 ZonedDateTime
创建格式化时间,格式化时区的gson对象。json解析时间格式化 ZonedDateTime
2022-06-28 06:10:00 【压力也没多大啊】
某次遇到不同模块给我送来的ZonedDateTime时间格式都不一样,我这边根据不同模块的类型new一个自定义的gson对象去解析json。
private Gson getMyGson() {
return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new JsonDeserializer<ZonedDateTime>() {
@Override
public ZonedDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (json == null) {
return null;
}
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(ZoneId.systemDefault());
return ZonedDateTime.from(fmt.parse(json.getAsString())).withZoneSameInstant(ZoneId.systemDefault());
}
}).registerTypeAdapter(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
@Override
public JsonElement serialize(ZonedDateTime zonedDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
return new JsonPrimitive(fmt.format(zonedDateTime.truncatedTo(ChronoUnit.MILLIS)));
}
}).setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setDateFormat("yyyy-MM-dd HH:mm:ss.SSS").create();
}
边栏推荐
- Object object to list collection
- What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?
- Iframe switching in Web Automation
- PKG package node project (express)
- 使用SQL select count distinct查询语句统计数据库中某个字段的唯一值总数量
- Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
- CAD二次开发+NetTopologySuite+PGIS 引用多版本DLL问题
- Working principle of es9023 audio decoding chip
- Development trend of mobile advertising: Leveraging stock and fine marketing
- Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
猜你喜欢

CAD secondary development +nettopologysuite+pgis reference multi version DLL

AutoCAD C polyline self intersection detection

Small ball playing

cocoapod中的第三方库怎么引用本地头文件

Paper recommendation: efficientnetv2 - get smaller models and faster training speed through NAS, scaling and fused mbconv

socke. IO long connection enables push, version control, and real-time active user statistics

Drop down box for implementation

Slow content advertising: the long-term principle of brand growth

链表(二)——设计链表

Filecoin hacker song developer competition
随机推荐
Socket. Io long Connection Push, version Control, Real - Time Active user volume Statistics
Syn retransmission caused by IPVS
death_ satan/hyperf-validate
5-minute NLP: summary of time chronology from bag of words to transformer
YYGH-BUG-02
bash install. SH ******** error
5g network overall architecture
马赛克数据增强 mosaic
使用SSM框架,配置多个数据库连接
YYGH-BUG-02
Install fmpefg
D3D11_ Chili_ Tutorial (3): design a bindable/drawable system
Paper recommendation: efficientnetv2 - get smaller models and faster training speed through NAS, scaling and fused mbconv
pytorch详解
Idea generates entity classes from database tables
Configure multiple database connections using the SSM framework
YYGH-6-微信登录
lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
socke. IO long connection enables push, version control, and real-time active user statistics
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException:异常解决