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

设置默认日期格式后,parseObject出错 #2529

Open
wghdir opened this issue May 6, 2024 · 2 comments
Open

设置默认日期格式后,parseObject出错 #2529

wghdir opened this issue May 6, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@wghdir
Copy link

wghdir commented May 6, 2024

问题描述

设置默认日期格式后,parseObject出错
Exception in thread "main" com.alibaba.fastjson.JSONException: parse date error, 2024-05-06 20:05:42, expect format yyyy-MM-dd HH:mm:ss.SSS
at com.alibaba.fastjson.JSON.parseObject(JSON.java:525)
at com.example.nativedemo.model.ResultVo.main(ResultVo.java:19)
Caused by: com.alibaba.fastjson2.JSONException: parse date error, 2024-05-06 20:05:42, expect format yyyy-MM-dd HH:mm:ss.SSS
at com.alibaba.fastjson2.JSONReader.readMillisFromString(JSONReader.java:1628)
at com.alibaba.fastjson2.reader.ObjectReaderImplDate.readDate(ObjectReaderImplDate.java:98)
at com.alibaba.fastjson2.reader.ObjectReaderImplDate.readObject(ObjectReaderImplDate.java:71)
at com.alibaba.fastjson2.reader.ORG_1_3_ResultVo.readObject(Unknown Source)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:515)
... 1 more

环境信息

  • OS信息: win11
  • JDK信息: graalvm17
  • 版本信息:Fastjson2 2.0.49 1.0兼容模式
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.49</version>
        </dependency>
public class ResultVo {
    public String code;
    public String msg;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    public Date requesttime;// 接收到请求时间

    public static void main(String[] args) {
        JSONObject.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";//设置日期格式
        ResultVo rv = ResultVo.sucessReturn("测试Date转换");
        String str = JSONObject.toJSONString(rv);
        ResultVo rvnew = JSON.parseObject(str, ResultVo.class);
        System.out.println(JSONObject.toJSONString(rvnew));
    }
    /**
     * 成功返回1,及消息
     *
     * @param retmsg
     * @return
     */
    public static ResultVo sucessReturn(String retmsg) {
        ResultVo rv = new ResultVo();
        rv.code = "200";
        rv.msg = retmsg;
        rv.requesttime = new Date();
        return rv;
    }
}
@wghdir wghdir added the bug Something isn't working label May 6, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 12, 2024
@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证,2.0.51版本预计在6月16日前发布。

@wenshao wenshao added the fixed label May 12, 2024
@wenshao
Copy link
Member

wenshao commented May 13, 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

2 participants