Skip to content

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

@wghdir

Description

@wghdir

问题描述

设置默认日期格式后,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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions