当前位置:网站首页>Create a gson object that formats the time zone. JSON parsing time formatting zoneddatetime
Create a gson object that formats the time zone. JSON parsing time formatting zoneddatetime
2022-06-28 06:13:00 【Not much pressure】
I was sent by different modules once ZonedDateTime Time format is different , According to the types of different modules new A custom gson Object to parse 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();
}
边栏推荐
- Idea automatically adds comments when creating classes
- Differences between overloads, rewrites, abstract classes and interfaces
- easyui 重置多条件查询
- OpenGL API learning (2008) client server client server
- The length of pytorch dataloader the difference between epoch and iteration
- API learning of OpenGL (2007) gltexcoordpointer
- Object对象转 List集合
- Linux MySQL implements root user login without password
- Alibaba cloud SMS service (Complete Guide), SMS sending function implementation.
- 记JPA使用自定义VO接收 JPQL查询结果
猜你喜欢

Xcode13.3.1 error reported after pod install
![Taobao seo training video course [22 lectures]](/img/81/21e844542b35010760d061abe905e9.jpg)
Taobao seo training video course [22 lectures]

Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes

Sharing tips for efficient scripting

Oracle fundamentals summary

Exception handling (I) -- null pointer and array index out of bounds

Example of MVVM framework based on kotlin+jetpack

19 fonctions de perte d'apprentissage profond

How to add live chat in your Shopify store?

Socket. Io long Connection Push, version Control, Real - Time Active user volume Statistics
随机推荐
Yolact++ Pytorch环境
MySQL (II) - basic operation
Linked list (I) - remove linked list elements
Linked list (II) - Design linked list
Deep learning 19 loss functions
pytorch详解
How the third-party libraries in cocoapod reference local header files
idea根据数据库表生成实体类
PKG package node project (express)
JDBC学习(一)——实现简单的CRUD操作
windows上安装redis并永久修改密码,及ssm框架集成redis
easyui 重置多条件查询
Yolact++ pytoch environment
High quality domestic stereo codec cjc8988, pin to pin replaces wm8988
How to add live chat in your Shopify store?
Linked list (III) - reverse linked list
death_ satan/hyperf-validate
Exception handling (I) -- null pointer and array index out of bounds
Apple MDM Bypass 免越狱绕过MDM配置锁 免费
MySQL common functions