fastjson官方可以提供对joda-time以及java8里面新增的时间类型的解析支持吗?
u3r8eeie1#
你用的是什么版本?目前是支持Java8新增日期类型的,但不支持joda-time时间类型,请问现在有了java8,用joda-time的还多么?
igsr9ssn2#
可以考虑支持一下啊,java8的功能相比joda的还是差一些
db2dz4w83#
@wenshao使用版本:1.1.58.android希望能够支持 joda-time,目前 Android 暂不正式支持 Java 8,所以只能引入 joda-time 依赖,Android 版本的 JSONField 似乎不支持 serializeUsing。
rjee0c154#
你好,不确定这个issue 是否仍在活跃,偶然搜索到这个问题。
在特定场合,特别是新老系统(java6/7系统 与 java8 新系统并存的情形下)之间的交互场景下,joda-time是一个非常有价值的辅助,故,fastjson对日期格式转换的支持还是有价值的。
As quick try out... java8 works pretty well, joda still not working yet. . .whereas ... jackson support well with addtional depenciese on jsr310 and joda datatype...
不管怎么样,可以参考 Jdk8DateCodec 实现一个简单的 codec 满足自己的场景。。。
@DaTapublic class Java8DateTime {@JSONField(format = "yyyy-MM-dd")@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")private LocalDate date;@JSONField(format = "HH:mm:ss")@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")private LocalTime time;@JSONField(format = "yyyy-MM-dd HH:mm:ss")@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")private LocalDateTime dateTime;}
@DaTapublic class JodaDateTime {@JSONField(format = "yyyy-mm-dd")@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")private LocalDate date;@JSONField(format = "HH:mm:ss")@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")private LocalTime time;@JSONField(format = "yyyy-MM-dd HH:mm:ss")@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")private LocalDateTime dateTime;}
java8:{"date":"2017-08-07","dateTime":"2017-08-07 20:13:11","time":"20:13:11"}
joda time:{"date":{"centuryOfEra":20,"chronology":{"zone":{"fixed":true,"iD":"UTC"}},"dayOfMonth":7,"dayOfWeek":1,"dayOfYear":219,"era":1,"fieldTypes":[{"durationType":{"name":"years"},"name":"year"},{"durationType":{"name":"months"},"name":"monthOfYear","rangeDurationType":{"$ref":"$.null.fieldTypes[0].durationType"}},{"durationType":{"name":"days"},"name":"dayOfMonth","rangeDurationType":{"$ref":"$.null.fieldTypes[1].durationType"}}],"fields":[{"durationField":{"name":"years","precise":false,"supported":true,"type":{"$ref":"$.null.fieldTypes[0].durationType"},"unitMillis":31556952000},"leapDurationField":{"name":"days","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[2].durationType"},"unitMillis":86400000},"lenient":false,"maximumValue":292278993,"minimumValue":-292275054,"name":"year","supported":true,"type":{"$ref":"$.null.fieldTypes[0]"}},{"durationField":{"name":"months","precise":false,"supported":true,"type":{"$ref":"$.null.fieldTypes[1].durationType"},"unitMillis":2629746000},"leapDurationField":{"$ref":"$.null.fields[0].leapDurationField"},"lenient":false,"maximumValue":12,"minimumValue":1,"name":"monthOfYear","rangeDurationField":{"$ref":"$.null.fields[0].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[1]"}},{"durationField":{"$ref":"$.null.fields[0].leapDurationField"},"lenient":false,"maximumValue":31,"minimumValue":1,"name":"dayOfMonth","rangeDurationField":{"$ref":"$.null.fields[1].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[2]"},"unitMillis":86400000}],"monthOfYear":8,"values":[2017,8,7],"weekOfWeekyear":32,"weekyear":2017,"year":2017,"yearOfCentury":17,"yearOfEra":2017},"dateTime":{"centuryOfEra":20,"chronology":{"$ref":"$.null.chronology"},"dayOfMonth":7,"dayOfWeek":1,"dayOfYear":219,"era":1,"fieldTypes":[{"$ref":"$.null.fieldTypes[0]"},{"$ref":"$.null.fieldTypes[1]"},{"$ref":"$.null.fieldTypes[2]"},{"durationType":{"name":"millis"},"name":"millisOfDay","rangeDurationType":{"$ref":"$.null.fieldTypes[2].durationType"}}],"fields":[{"$ref":"$.null.fields[0]"},{"$ref":"$.null.fields[1]"},{"$ref":"$.null.fields[2]"},{"durationField":{"name":"millis","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[3].durationType"},"unitMillis":1},"lenient":false,"maximumValue":86399999,"minimumValue":0,"name":"millisOfDay","range":86400000,"rangeDurationField":{"$ref":"$.null.fields[0].leapDurationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[3]"},"unitMillis":1}],"hourOfDay":20,"millisOfDay":72792351,"millisOfSecond":351,"minuteOfHour":13,"monthOfYear":8,"secondOfMinute":12,"values":[2017,8,7,72792351],"weekOfWeekyear":32,"weekyear":2017,"year":2017,"yearOfCentury":17,"yearOfEra":2017},"time":{"chronology":{"$ref":"$.null.chronology"},"fieldTypes":[{"durationType":{"name":"hours"},"name":"hourOfDay","rangeDurationType":{"$ref":"$.null.fieldTypes[2].durationType"}},{"durationType":{"name":"minutes"},"name":"minuteOfHour","rangeDurationType":{"$ref":"$.null.fieldTypes[0].durationType"}},{"durationType":{"name":"seconds"},"name":"secondOfMinute","rangeDurationType":{"$ref":"$.null.fieldTypes[1].durationType"}},{"durationType":{"$ref":"$.null.fieldTypes[3].durationType"},"name":"millisOfSecond","rangeDurationType":{"$ref":"$.null.fieldTypes[2].durationType"}}],"fields":[{"durationField":{"name":"hours","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[0].durationType"},"unitMillis":3600000},"lenient":false,"maximumValue":23,"minimumValue":0,"name":"hourOfDay","range":24,"rangeDurationField":{"$ref":"$.null.fields[0].leapDurationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[0]"},"unitMillis":3600000},{"durationField":{"name":"minutes","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[1].durationType"},"unitMillis":60000},"lenient":false,"maximumValue":59,"minimumValue":0,"name":"minuteOfHour","range":60,"rangeDurationField":{"$ref":"$.null.fields[0].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[1]"},"unitMillis":60000},{"durationField":{"name":"seconds","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[2].durationType"},"unitMillis":1000},"lenient":false,"maximumValue":59,"minimumValue":0,"name":"secondOfMinute","range":60,"rangeDurationField":{"$ref":"$.null.fields[1].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[2]"},"unitMillis":1000},{"durationField":{"$ref":"$.null.fields[3].durationField"},"lenient":false,"maximumValue":999,"minimumValue":0,"name":"millisOfSecond","range":1000,"rangeDurationField":{"$ref":"$.null.fields[2].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[3]"},"unitMillis":1}],"hourOfDay":20,"millisOfDay":72792347,"millisOfSecond":347,"minuteOfHour":13,"secondOfMinute":12,"values":[20,13,12,347]}}\
jackson try:
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda', version: '2.9.0'compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.0'
@slf4jpublic class DateTimeJacksonTest {public static void main(String[] args) throws IOException {ObjectMapper objectMapper = new ObjectMapper();objectMapper.registerModule(new JavaTimeModule());objectMapper.registerModule(new JodaModule());objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Java8DateTime java8DateTime = new Java8DateTime(); java8DateTime.setDate(LocalDate.now()); java8DateTime.setTime(LocalTime.now()); java8DateTime.setDateTime(LocalDateTime.now()); String json = objectMapper.writeValueAsString(java8DateTime); System.out.println(json); JodaDateTime jodaDateTime = new JodaDateTime(); jodaDateTime.setDate(org.joda.time.LocalDate.now()); jodaDateTime.setTime(org.joda.time.LocalTime.now()); jodaDateTime.setDateTime(org.joda.time.LocalDateTime.now()); String json2 = objectMapper.writeValueAsString(jodaDateTime); System.out.println(json2); JodaDateTime parse = objectMapper.readValue(json, JodaDateTime.class); System.out.println(parse); }
}--- output:{"date":"07-08-2017","time":"08:32:00","dateTime":"07-08-2017 08:32:00"}{"date":"07-08-2017","time":"08:32:00","dateTime":"07-08-2017 08:32:00"}JodaDateTime(date=2017-08-07, time=08:32:00.000, dateTime=2017-08-07T08:32:00.000)
4条答案
按热度按时间u3r8eeie1#
你用的是什么版本?目前是支持Java8新增日期类型的,但不支持joda-time时间类型,请问现在有了java8,用joda-time的还多么?
igsr9ssn2#
可以考虑支持一下啊,java8的功能相比joda的还是差一些
db2dz4w83#
@wenshao
使用版本:1.1.58.android
希望能够支持 joda-time,目前 Android 暂不正式支持 Java 8,所以只能引入 joda-time 依赖,Android 版本的 JSONField 似乎不支持 serializeUsing。
rjee0c154#
你好,不确定这个issue 是否仍在活跃,偶然搜索到这个问题。
在特定场合,特别是新老系统(java6/7系统 与 java8 新系统并存的情形下)之间的交互场景下,joda-time是一个非常有价值的辅助,故,fastjson对日期格式转换的支持还是有价值的。
As quick try out... java8 works pretty well, joda still not working yet. . .
whereas ... jackson support well with addtional depenciese on jsr310 and joda datatype...
不管怎么样,可以参考 Jdk8DateCodec 实现一个简单的 codec 满足自己的场景。。。
@DaTa
public class Java8DateTime {
@JSONField(format = "yyyy-MM-dd")
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private LocalDate date;
@JSONField(format = "HH:mm:ss")
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")
private LocalTime time;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
private LocalDateTime dateTime;
}
@DaTa
public class JodaDateTime {
@JSONField(format = "yyyy-mm-dd")
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private LocalDate date;
@JSONField(format = "HH:mm:ss")
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")
private LocalTime time;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
private LocalDateTime dateTime;
}
java8:
{"date":"2017-08-07","dateTime":"2017-08-07 20:13:11","time":"20:13:11"}
joda time:
{"date":{"centuryOfEra":20,"chronology":{"zone":{"fixed":true,"iD":"UTC"}},"dayOfMonth":7,"dayOfWeek":1,"dayOfYear":219,"era":1,"fieldTypes":[{"durationType":{"name":"years"},"name":"year"},{"durationType":{"name":"months"},"name":"monthOfYear","rangeDurationType":{"$ref":"$.null.fieldTypes[0].durationType"}},{"durationType":{"name":"days"},"name":"dayOfMonth","rangeDurationType":{"$ref":"$.null.fieldTypes[1].durationType"}}],"fields":[{"durationField":{"name":"years","precise":false,"supported":true,"type":{"$ref":"$.null.fieldTypes[0].durationType"},"unitMillis":31556952000},"leapDurationField":{"name":"days","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[2].durationType"},"unitMillis":86400000},"lenient":false,"maximumValue":292278993,"minimumValue":-292275054,"name":"year","supported":true,"type":{"$ref":"$.null.fieldTypes[0]"}},{"durationField":{"name":"months","precise":false,"supported":true,"type":{"$ref":"$.null.fieldTypes[1].durationType"},"unitMillis":2629746000},"leapDurationField":{"$ref":"$.null.fields[0].leapDurationField"},"lenient":false,"maximumValue":12,"minimumValue":1,"name":"monthOfYear","rangeDurationField":{"$ref":"$.null.fields[0].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[1]"}},{"durationField":{"$ref":"$.null.fields[0].leapDurationField"},"lenient":false,"maximumValue":31,"minimumValue":1,"name":"dayOfMonth","rangeDurationField":{"$ref":"$.null.fields[1].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[2]"},"unitMillis":86400000}],"monthOfYear":8,"values":[2017,8,7],"weekOfWeekyear":32,"weekyear":2017,"year":2017,"yearOfCentury":17,"yearOfEra":2017},"dateTime":{"centuryOfEra":20,"chronology":{"$ref":"$.null.chronology"},"dayOfMonth":7,"dayOfWeek":1,"dayOfYear":219,"era":1,"fieldTypes":[{"$ref":"$.null.fieldTypes[0]"},{"$ref":"$.null.fieldTypes[1]"},{"$ref":"$.null.fieldTypes[2]"},{"durationType":{"name":"millis"},"name":"millisOfDay","rangeDurationType":{"$ref":"$.null.fieldTypes[2].durationType"}}],"fields":[{"$ref":"$.null.fields[0]"},{"$ref":"$.null.fields[1]"},{"$ref":"$.null.fields[2]"},{"durationField":{"name":"millis","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[3].durationType"},"unitMillis":1},"lenient":false,"maximumValue":86399999,"minimumValue":0,"name":"millisOfDay","range":86400000,"rangeDurationField":{"$ref":"$.null.fields[0].leapDurationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[3]"},"unitMillis":1}],"hourOfDay":20,"millisOfDay":72792351,"millisOfSecond":351,"minuteOfHour":13,"monthOfYear":8,"secondOfMinute":12,"values":[2017,8,7,72792351],"weekOfWeekyear":32,"weekyear":2017,"year":2017,"yearOfCentury":17,"yearOfEra":2017},"time":{"chronology":{"$ref":"$.null.chronology"},"fieldTypes":[{"durationType":{"name":"hours"},"name":"hourOfDay","rangeDurationType":{"$ref":"$.null.fieldTypes[2].durationType"}},{"durationType":{"name":"minutes"},"name":"minuteOfHour","rangeDurationType":{"$ref":"$.null.fieldTypes[0].durationType"}},{"durationType":{"name":"seconds"},"name":"secondOfMinute","rangeDurationType":{"$ref":"$.null.fieldTypes[1].durationType"}},{"durationType":{"$ref":"$.null.fieldTypes[3].durationType"},"name":"millisOfSecond","rangeDurationType":{"$ref":"$.null.fieldTypes[2].durationType"}}],"fields":[{"durationField":{"name":"hours","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[0].durationType"},"unitMillis":3600000},"lenient":false,"maximumValue":23,"minimumValue":0,"name":"hourOfDay","range":24,"rangeDurationField":{"$ref":"$.null.fields[0].leapDurationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[0]"},"unitMillis":3600000},{"durationField":{"name":"minutes","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[1].durationType"},"unitMillis":60000},"lenient":false,"maximumValue":59,"minimumValue":0,"name":"minuteOfHour","range":60,"rangeDurationField":{"$ref":"$.null.fields[0].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[1]"},"unitMillis":60000},{"durationField":{"name":"seconds","precise":true,"supported":true,"type":{"$ref":"$.null.fieldTypes[2].durationType"},"unitMillis":1000},"lenient":false,"maximumValue":59,"minimumValue":0,"name":"secondOfMinute","range":60,"rangeDurationField":{"$ref":"$.null.fields[1].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[2]"},"unitMillis":1000},{"durationField":{"$ref":"$.null.fields[3].durationField"},"lenient":false,"maximumValue":999,"minimumValue":0,"name":"millisOfSecond","range":1000,"rangeDurationField":{"$ref":"$.null.fields[2].durationField"},"supported":true,"type":{"$ref":"$.null.fieldTypes[3]"},"unitMillis":1}],"hourOfDay":20,"millisOfDay":72792347,"millisOfSecond":347,"minuteOfHour":13,"secondOfMinute":12,"values":[20,13,12,347]}}\
jackson try:
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda', version: '2.9.0'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.0'
@slf4j
public class DateTimeJacksonTest {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new JodaModule());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
--- output:
{"date":"07-08-2017","time":"08:32:00","dateTime":"07-08-2017 08:32:00"}
{"date":"07-08-2017","time":"08:32:00","dateTime":"07-08-2017 08:32:00"}
JodaDateTime(date=2017-08-07, time=08:32:00.000, dateTime=2017-08-07T08:32:00.000)