Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 序列化LocalDateTime字段时报错 #2206

Closed
yao147258 opened this issue Jan 22, 2024 · 5 comments
Closed

[BUG] 序列化LocalDateTime字段时报错 #2206

yao147258 opened this issue Jan 22, 2024 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@yao147258
Copy link

yao147258 commented Jan 22, 2024

问题描述

简要描述您碰到的问题。
序列化
@jsonformat(
pattern = "yyyy-MM-dd'T'HH:mm:ssXXX",
timezone = "GMT+8"
)
private LocalDateTime birthday;
这样的字段时会出现以下错误

Exception in thread "main" com.alibaba.fastjson.JSONException: toJSONString error
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:1498)
at com.yzy.portal.YfqTest.main(YfqTest.java:28)
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
at java.time.LocalDate.get0(LocalDate.java:680)
at java.time.LocalDate.getLong(LocalDate.java:659)
at java.time.LocalDateTime.getLong(LocalDateTime.java:720)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3346)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2190)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
at com.alibaba.fastjson2.writer.ObjectWriterImplLocalDateTime.write(ObjectWriterImplLocalDateTime.java:121)
at com.alibaba.fastjson2.writer.FieldWriterObjectFinal.write(FieldWriterObjectFinal.java:83)
at com.alibaba.fastjson2.writer.ObjectWriter1.write(ObjectWriter1.java:68)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:1490)
... 1 more

Process finished with exit code 1

环境信息

请填写以下信息:

  • JDK信息:jdk1.8
  • 版本信息:Fastjson2 2.0.45
public static void main(String[] args) {
        User user = new User();
        user.setBirthday(LocalDateTime.now());
        System.out.println(JSON.toJSONString(user));
   }

 @Data
    private static class User {
        @JsonFormat(
                pattern = "yyyy-MM-dd'T'HH:mm:ssXXX",
                timezone = "GMT+8"
        )
        private LocalDateTime birthday;
    }
@yao147258 yao147258 added the bug Something isn't working label Jan 22, 2024
@yao147258 yao147258 reopened this Jan 22, 2024
@yao147258 yao147258 changed the title [BUG] [BUG] 序列化LocalDateTime字段时报错 Jan 22, 2024
@yanxutao89
Copy link
Collaborator

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"))
本身就不支持OffsetSeconds吧?

@yao147258
Copy link
Author

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")) 本身就不支持OffsetSeconds吧?

是新版本的不支持了吗,昨天从1点几版本升到2.0.45发现解析不了了

@yanxutao89
Copy link
Collaborator

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")) 本身就不支持OffsetSeconds吧?

是新版本的不支持了吗,昨天从1点几版本升到2.0.45发现解析不了了

麻烦发下可以支持的版本号

@yao147258
Copy link
Author

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")) 本身就不支持OffsetSeconds吧?

是新版本的不支持了吗,昨天从1点几版本升到2.0.45发现解析不了了

麻烦发下可以支持的版本号

com.alibaba fastjson 1.2.79 这个版本我试了是可以的

@wenshao wenshao added this to the 2.0.46 milestone Jan 27, 2024
@wenshao wenshao added the fixed label Jan 27, 2024
@wenshao
Copy link
Member

wenshao commented Jan 29, 2024

@wenshao wenshao closed this as completed Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants