fastjson 时间格式问题2018-05-21T14:39:44.9077913+08:00

knsnq2tg  于 2022-11-05  发布在  其他
关注(0)|答案(7)|浏览(468)

2018-05-21T14:39:44.9077913+08:00对接第三方接口,对方的时间格式是这样的,java里面不知道以什么方式接收,试了"yyyy-MM-dd'T'HH:mm:ss.SSSSSXXX" 和 "yyyy-MM-dd'T'HH:mm:ss.sssssssXXX",结果都不准确,求教!!

p5cysglq

p5cysglq1#

9077913 这个值过大,不是毫秒数,这个是什么格式?

jexiocij

jexiocij2#

和第三方对接api时返回的时间格式,对方是用C#开发的。

目前的解决方法是使用LocalDateTime

@JSONField(format = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX")
private LocalDateTime xxxDate;

但是这样也只能解析和S个数相同的时间

2018-05-21T14:39:44.9077913+08:00 解析成功
2018-05-21T14:39:44.907791+08:00 解析失败

第三方返回的S长度又不固定,比较难解决

zbsbpyhn

zbsbpyhn3#

我也遇到这个问题了,也是跟.net系统交互的时候,对方序列化过来的时间格式是:
"2019-01-25T17:34:13.1920977+08:00"
这边fastjson反序列化失败

zpqajqem

zpqajqem4#

static class Model{
        public Date date;
    }

    public static void main(String[] args) throws Exception {
        String json = "{\"date\":\"2019-01-25T17:34:13.1920977+08:00\"}";

        System.out.println("jackson:" + new ObjectMapper().readValue(json, Model.class).date);
        System.out.println("fastjson:" + JSON.parseObject(json, Model.class).date);
    }

@wenshao jackson(2.9.8)可以正确解析,fastjson(1.2.56)无法解析

jackson:Fri Jan 25 17:34:13 CST 2019

Exception in thread "main" com.alibaba.fastjson.JSONException: For input string: "2019-01-25T17:34:13.1920977+08:00"
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:680)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:369)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:273)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:546)
at work.darren.Test.main(Test.java:29)
Caused by: java.lang.NumberFormatException: For input string: "2019-01-25T17:34:13.1920977+08:00"

nkhmeac6

nkhmeac65#

最新版本 1.2.59,已经解决。

@JSONField(format = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX")
o2g1uqev

o2g1uqev6#

1.2.59

@Data
public class LocalModel {

    @JSONField(format = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX")
    private LocalDateTime localDateTime;

    @JSONField(format = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX")
    private LocalDateTime localDateTime2;

    public static void main(String[] args) {
        String json = "{\n" +
                "\t\"localDateTime\":\"2018-05-21T14:39:44.9077913+08:00\"\n" +
                "}";
        LocalModel localModel = JSONObject.parseObject(json, LocalModel.class);
        System.out.println(JSON.toJSONString(localModel));
    }
}

报错

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
	at java.time.LocalDate.get0(LocalDate.java:680)
	at java.time.LocalDate.getLong(LocalDate.java:659)
	at java.time.LocalDateTime.getLong(LocalDateTime.java:720)
	at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
	at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3335)
	at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
	at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
	at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
	at com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec.write(Jdk8DateCodec.java:484)
	at com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec.write(Jdk8DateCodec.java:467)
	at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:287)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:454)
	... 6 more

@Omega-Ariston

jv4diomz

jv4diomz7#

这是因为 java.time.LocalDateTime 不支持时区偏移量, 也就是 +08:00 . 建议使用 java.time.OffsetDateTime

@lombok.Data
public class LocalModel {

    @com.alibaba.fastjson.annotation.JSONField(format = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX")
    private java.time.OffsetDateTime localDateTime;

    @com.alibaba.fastjson.annotation.JSONField(format = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX")
    private java.time.OffsetDateTime localDateTime2;

    public static void main(String[] args) {
        String json = "{\n" +
                "\t\"localDateTime\":\"2018-05-21T14:39:44.9077913+08:00\"\n" +
                "}";
        LocalModel localModel = com.alibaba.fastjson.JSONObject.parseObject(json, LocalModel.class);
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(localModel));
    }
}

@CielDai

相关问题