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]对象转JsonString后,时间发生变化 #2650

Open
sansizl opened this issue May 31, 2024 · 1 comment
Open

[BUG]对象转JsonString后,时间发生变化 #2650

sansizl opened this issue May 31, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@sansizl
Copy link

sansizl commented May 31, 2024

问题描述

简要描述您碰到的问题。
fastjson版本从1.2.83升级到2.0.49之后,日期转为jsonString时,日期发生改变

环境信息

请填写以下信息:

  • OS信息: [e.g.:Windows 11]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson 2.0.49]

重现步骤

如何操作可以重现该问题:

    String dateStr = "1900-01-01 00:06:43.0";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date date = simpleDateFormat.parse(dateStr);
    System.out.println(date);
    System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));

image

相关日志输出

请复制并粘贴任何相关的日志输出。
打印结果如下:
image

@sansizl sansizl added the bug Something isn't working label May 31, 2024
@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

对于1900-1-1的时区,java.time和SimpleDateFormat的处理方式不一样。fastjson2使用的是java.time API。

        String dateStr = "1900-01-01 00:06:43.0";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        Date date = simpleDateFormat.parse(dateStr);
        System.out.println(date);
        System.out.println(date.toInstant().atZone(ZoneId.of("Asia/Shanghai")));
        System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));

System.out.println(date.toInstant().atZone(ZoneId.of("Asia/Shanghai"))); 打印的结果就是fastjson2的结果

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants