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

fastjson在序列化JDK8的LocalDateTime是,无法设置日期格式. #942

Closed
sunxiang0918 opened this issue Dec 8, 2016 · 7 comments
Closed
Labels
Milestone

Comments

@sunxiang0918
Copy link

使用的是 1.2.21 这个版本.

不管我设不设置 SerializerFeature.WriteDateUseDateFormat 或者调用JSON.toJSONStringWithDateFormat 都没有其效果. 最后生成的JSON还是 LocalDateTime.toString 的样式.

翻了一下源码. 发现

Jdk8DateCodec#write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                      int features)

这个类. 在直接调用 JSON.toJSONStringWithDateFormat(LocalDateTime.now())的时候,传入的fieldType为null,导致走到 out.writeString(object.toString()); 去了. 从而生成的json为LocalDateTime.toString的格式.

而如果是在一个JAVABean中有LocalDateTime字段,进行序列化的时候. 传入的fieldType是正常的LocalDateTime.class . 但是 if (dateTime.getNano() == 0) 这句过不到,导致还是走到out.writeString(object.toString());中去了.从而生成的json为LocalDateTime.toString的格式.
不大理解 if (dateTime.getNano() == 0) 这句话在这的目的是什么~ 只要我实例化LocalDateTime的时候使用LocalDateTime.now().withNano(0) 就可以格式化时间了. 谢谢!

@wenshao wenshao added the bug label Dec 9, 2016
@wenshao wenshao added this to the 1.2.22 milestone Dec 9, 2016
@wenshao
Copy link
Member

wenshao commented Dec 9, 2016

请提供重现问题的testcase

@sunxiang0918
Copy link
Author

sunxiang0918 commented Dec 10, 2016

测试用例:

    @Test
    public void testCase() throws Exception {

        System.out.println("===直接序列化LocalDateTime,无论如何格式不可变化===");
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(JSON.toJSONString(dateTime));
        /*期望是 yyyy-MM-dd HH:mm:ss   实际是 LocalDateTime.toString() 的格式 */
        System.out.println(JSON.toJSONString(dateTime,SerializerFeature.WriteDateUseDateFormat));
        System.out.println(JSON.toJSONStringWithDateFormat(dateTime,"yyyy-MM-dd HH:mm:ss"));

        System.out.println("===直接序列化LocalDateTime.withNano(0),无论如何格式不可变化===");
        dateTime = LocalDateTime.now().withNano(0);
        System.out.println(JSON.toJSONString(dateTime));
        /*期望是 yyyy-MM-dd HH:mm:ss   实际是 LocalDateTime.toString() 的格式 */
        System.out.println(JSON.toJSONString(dateTime,SerializerFeature.WriteDateUseDateFormat));
        System.out.println(JSON.toJSONStringWithDateFormat(dateTime,"yyyy-MM-dd HH:mm:ss"));

        System.out.println("==序列化VO对象中包含LocalDateTime,无论如何格式不可变化==");
        LocalDateTimeModel localDateTimeModel = new LocalDateTimeModel();
        System.out.println(JSON.toJSONString(localDateTimeModel));
        /*期望是 yyyy-MM-dd HH:mm:ss   实际是 LocalDateTime.toString() 的格式 */
        System.out.println(JSON.toJSONString(localDateTimeModel,SerializerFeature.WriteDateUseDateFormat));
        System.out.println(JSON.toJSONStringWithDateFormat(localDateTimeModel,"yyyy-MM-dd HH:mm:ss"));

        System.out.println("==序列化VO对象中包含LocalDateTime.withNano(0),日期格式变化正确==");
        localDateTimeModel = new LocalDateTimeModel();
        localDateTimeModel.setStartDateTime(localDateTimeModel.getStartDateTime().withNano(0));
        localDateTimeModel.setEndDateTime(localDateTimeModel.getEndDateTime().withNano(0));
        System.out.println(JSON.toJSONString(localDateTimeModel));
        System.out.println(JSON.toJSONString(localDateTimeModel,SerializerFeature.WriteDateUseDateFormat));
        System.out.println(JSON.toJSONStringWithDateFormat(localDateTimeModel,"yyyy-MM-dd HH:mm:ss"));     
    }

所使用的model:

class LocalDateTimeModel implements Serializable {
    
    private LocalDateTime startDateTime;
    
    private LocalDateTime endDateTime;
    
    private Date startDate;
    
    private Date endDate;

    public LocalDateTimeModel() {
        startDateTime = LocalDateTime.now();
        endDateTime = LocalDateTime.now().plusWeeks(1);

        startDate = new Date();
        endDate = new Date(startDate.getTime()+36000000);
    }

    public LocalDateTime getStartDateTime() {
        return startDateTime;
    }

    public void setStartDateTime(LocalDateTime startDateTime) {
        this.startDateTime = startDateTime;
    }

    public LocalDateTime getEndDateTime() {
        return endDateTime;
    }

    public void setEndDateTime(LocalDateTime endDateTime) {
        this.endDateTime = endDateTime;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
}

结果:

===直接序列化LocalDateTime,无论如何格式不可变化===
"2016-12-10T11:15:33.308"
"2016-12-10T11:15:33.308"
"2016-12-10T11:15:33.308"
===直接序列化LocalDateTime.withNano(0),无论如何格式不可变化===
"2016-12-10T11:15:33"
"2016-12-10T11:15:33"
"2016-12-10T11:15:33"
==序列化VO对象中包含LocalDateTime,无论如何格式不可变化==
{"endDate":1481375733393,"endDateTime":"2016-12-17T11:15:33.393","startDate":1481339733393,"startDateTime":"2016-12-10T11:15:33.393"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17T11:15:33.393","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10T11:15:33.393"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17T11:15:33.393","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10T11:15:33.393"}
==序列化VO对象中包含LocalDateTime.withNano(0),日期格式变化正确==
{"endDate":1481375733428,"endDateTime":"2016-12-17 11:15:33","startDate":1481339733428,"startDateTime":"2016-12-10 11:15:33"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17 11:15:33","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10 11:15:33"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17 11:15:33","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10 11:15:33"}

@wenshao wenshao modified the milestones: 1.2.23, 1.2.22 Dec 10, 2016
@ghost
Copy link

ghost commented Dec 11, 2016

还有toJavaObject的反序列化方法也存在问题,尽快修复下。
parseObject是正常的。

@wenshao
Copy link
Member

wenshao commented Dec 19, 2016

1.2.23中问题已经修复,会尽快发布新版本

@KeZhimin
Copy link

Fastjson把对象转为json时候,Date会转为时间戳,那LocalDateTime怎么转为时间戳呢

@dudw
Copy link

dudw commented Feb 12, 2019

同问,LocalDateTime怎么转为时间戳?

@lizd002
Copy link

lizd002 commented May 23, 2020

追问,LocalDateTime怎么转换为时间戳呀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants