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] 纳秒精度的时间字符串,反序列化成Date类型不兼容 #3210

Open
zyhui98 opened this issue Dec 16, 2024 · 5 comments
Open
Labels
bug Something isn't working fixed
Milestone

Comments

@zyhui98
Copy link

zyhui98 commented Dec 16, 2024

问题描述

原因是升级JDK21之后,LocalDateTime默认精度从毫秒升级到纳秒了。

重现代码如下:

@Data
public class TestBean {
    private Date time;
}

public static void main(String[] args) throws Exception {
        String s = "{\"time\":\"2024-12-04T20:43:15.000000999\"}";
        System.out.println(JSONObject.parseObject(s, TestBean.class));
}

异常堆栈:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2024-12-04T20:43:15.000000999' could not be parsed, unparsed text found at index 19
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2111)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:2010)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
at com.alibaba.fastjson2.reader.ObjectReaderImplDate.readDate(ObjectReaderImplDate.java:148)
at com.alibaba.fastjson2.reader.ObjectReaderImplDate.readObject(ObjectReaderImplDate.java:48)
at com.alibaba.fastjson2.reader.ORG_1_1_TestBean.readObject(Unknown Source)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:542)

@zyhui98 zyhui98 added the bug Something isn't working label Dec 16, 2024
@wenshao wenshao added this to the 2.0.54 milestone Dec 23, 2024
@wenshao
Copy link
Member

wenshao commented Dec 23, 2024

你用的是什么版本,我测试下来是支持的

wenshao added a commit that referenced this issue Dec 23, 2024
@zyhui98
Copy link
Author

zyhui98 commented Dec 26, 2024

你用的是什么版本,我测试下来是支持的
2.0.53

@zyhui98
Copy link
Author

zyhui98 commented Dec 26, 2024

1.2.70也不行的。

@zyhui98
Copy link
Author

zyhui98 commented Dec 26, 2024

你用的是什么版本,我测试下来是支持的

请问,你是什么版本,我试试

@wenshao
Copy link
Member

wenshao commented Dec 27, 2024

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import org.junit.jupiter.api.Test;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class Issue3210 {
    @Data
    public static class Bean {
        private Date time;
    }
    @Test
    public void test() {
        String s = "{\"time\":\"2024-12-04T20:43:15.000000999\"}";
        Bean bean = JSONObject.parseObject(s, Bean.class);
        assertNotNull(bean.time);
    }
}

当前的main分支的代码,我测试是好的

@wenshao wenshao added the fixed label Jan 1, 2025
wenshao added a commit that referenced this issue Jan 1, 2025
wenshao added a commit that referenced this issue Jan 1, 2025
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