Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Timestamp 反序列化问题 #414

Closed
tungSing opened this issue Sep 7, 2015 · 3 comments
Closed

Timestamp 反序列化问题 #414

tungSing opened this issue Sep 7, 2015 · 3 comments
Milestone

Comments

@tungSing
Copy link

tungSing commented Sep 7, 2015

我在做timestamp类型日期的反序列化遇到一个问题,错误信息如下

Exception in thread "main" com.alibaba.fastjson.JSONException: For input string: "2015-09-07"
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:555)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:250)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:226)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:185)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:303)

代码如下

    public static void main(String[] args) {
        String jsonStr = "{publishedDate:\"2015-09-07\"}";
        TestEntity news = JSON.parseObject(jsonStr, TestEntity.class);
        System.out.println(news.getPublishedDate());
    }
public class TestEntity {
    private Timestamp publishedDate;

    public Timestamp getPublishedDate() {
        return publishedDate;
    }
    public void setPublishedDate(Timestamp publishedDate) {
        this.publishedDate = publishedDate;
    }
}

1.通过设置全局日期格式可以解决,但这个业务格式要求和全局不一样,所以无法改变全局

    JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";

2.通过设置解析器也能解决

TestEntity news = JSON.parseObject(jsonStr, TestEntity.class,Feature.AllowISO8601DateFormat);

问:如何设置全局的解析器,不用每次都要传递参数

3.我尝试用注解,没有成功,代码如下

    @JSONField(format="yyyy-MM-dd",parseFeatures={Feature.AllowISO8601DateFormat})
    private Timestamp publishedDate;

问:注解能成功吗

@qifanyang
Copy link
Contributor

String jsonStr = "{publishedDate:"2015-09-07 00:00:00"}";

你可以看下#401

@wenshao wenshao added this to the 1.2.9 milestone Apr 11, 2016
@wenshao wenshao closed this as completed Apr 15, 2016
wenshao added a commit that referenced this issue Apr 15, 2016
@RishonLi
Copy link

RishonLi commented Jan 24, 2018

1.2.7版本有此问题
1.2.24版本此问题已经被修复

@houdq
Copy link

houdq commented May 10, 2018

英文时间格式转化还有问题吧

asa1997 pushed a commit to Be-Secure/fastjson that referenced this issue Feb 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants