当前位置:网站首页>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();
}
边栏推荐
- 使用pytorch和tensorflow计算分类模型的混淆矩阵
- 深度学习19种损失函数
- How the third-party libraries in cocoapod reference local header files
- Use of JDBC
- 链表(二)——设计链表
- Caused by: com. fasterxml. jackson. databind. Exc.invalidformatexception: exception resolution
- Yolact++ pytoch environment
- lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
- Idea generates entity classes from database tables
- Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
猜你喜欢
AutoCAD C polyline small acute angle detection
Alert pop-up processing in Web Automation
Paper recommendation: efficientnetv2 - get smaller models and faster training speed through NAS, scaling and fused mbconv
What is webrtc?
Ethereum Classic的难度计算|猿创征文
Use of JDBC
Iframe switching in Web Automation
Linked list (I) - remove linked list elements
Unity packaging webgl uses IIS to solve the error
Error reporting - resolve core JS / modules / es error. cause. JS error
随机推荐
AttributeError: 'callable_iterator' object has no attribute 'next'
pytorch详解
Slow content advertising: the long-term principle of brand growth
JDBC learning (I) -- implementing simple CRUD operations
AutoCAD C# 多段线自相交检测
Introduction to openscap
pkg打包node工程(express)
Online facing such an online world, the only limitation is our imagination
简单手写debounce函数
High quality domestic stereo codec cjc8988, pin to pin replaces wm8988
No one can only use foreach to traverse arrays, right?
Socket. Io long Connection Push, version Control, Real - Time Active user volume Statistics
手把手教你用Ucos
Sharing tips for efficient scripting
Is it safe to open a stock account? How to open a stock account?
windows上安装redis并永久修改密码,及ssm框架集成redis
Drop down list processing in Web Automation
【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
链表(二)——设计链表
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]]