我正在尝试做一个简单的练习,使用更快的XML(杰克逊)https://github.com/FasterXML/jackson 将字符串转换为Joda DateTime
。如果字符串格式像"1972-12-28T12:00:01.000Z",我可以转换它。但是一旦我将格式转换为简单的"09/23/2016"(MM/dd/yyyy),代码就会失败。以下是我的代码:
public static void main( String[] args )
throws JsonParseException, JsonMappingException, IOException {
final String INPUT_JSON = "{"start" : "11/23/2016"}";
// final String INPUT_JSON = "{"start" : "1972-12-28T12:00:01.000Z"}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule( new JodaModule() );
DateFormat dateFormat = new SimpleDateFormat( "MM/dd/yyyy" );
objectMapper.setDateFormat( dateFormat );
Bean bean = objectMapper.readValue( INPUT_JSON, Bean.class );
DateTime start = bean.getStart();
System.out.println( start );
}
public class Bean {
public DateTime start;
public Bean() {
// TODO Auto-generated constructor stub
}
public DateTime getStart() {
return start;
}
public void setStart( DateTime start ) {
this.start = start;
}
}
例外情况如下:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Invalid format: "11/23/2016" is malformed at "/23/2016" (through reference chain: com.firstfuel.analytics.bpu.translator.Bean["start"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1420)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:244)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2986)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2091)
at com.firstfuel.analytics.bpu.translator.VTModelPostProcessRequestTranslator.main(VTModelPostProcessRequestTranslator.java:125)
Caused by: java.lang.IllegalArgumentException: Invalid format: "11/23/2016" is malformed at "/23/2016"
at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:754)
at org.joda.time.convert.StringConverter.getInstantMillis(StringConverter.java:65)
at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:150)
at org.joda.time.DateTime.<init>(DateTime.java:265)
at com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer.deserialize(DateTimeDeserializer.java:48)
at com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer.deserialize(DateTimeDeserializer.java:20)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
... 4 more
这看起来很简单,但我不确定为什么会有问题。我无法将日期格式更改为"MM/dd/yyyy"以外的任何格式。我使用了 Java Date 并且可以工作,但我无法将数据保存到数据库中,因为这些列在实体对象中映射为 Joda LocalDate,并且当我将日期转换为 LocalDate 时,我遇到了持续的问题。但是我们不要去那里。如果有人可以帮助我解决上述问题,我将不胜感激。
这个问题已经回答了,但我想与您分享一种无需太多代码行即可解决此问题的替代方法。只需将@JsonFormat注释添加到 pojo 属性即可。
@JsonFormat( pattern="MM/dd/yyyy")
public DateTime start;
并序列化时间部分
@JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")
public DateTime field = new DateTime();
您还需要将JodaModule注册到杰克逊才能利用它。
final ObjectMapper result = new ObjectMapper();
result.registerModule(new JodaModule());
有关此模块的模式详细信息,请参阅 https://github.com/FasterXML/jackson-datatype-joda
最后说明:Java8 SE 包含一个新的日期和时间库,因此可以避免使用 Joda 时间库(更多详细信息 http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html)。为了开始使用它,你需要注册模块JavaTimeModule而不是JodaModule。它包含在"jackson-datatype-jsr310"中
最后我开始工作了。我使用自定义反序列化程序来使其工作。以下是代码:
有问题的豆子
public class Bean {
@JsonDeserialize(using = JSONDateAdapter.class)
public DateTime start;
public Bean() {
// TODO Auto-generated constructor stub
}
public DateTime getStart() {
return start;
}
public void setStart( DateTime start ) {
this.start = start;
}
}
适配器:
public class JSONLocalDateAdapter
extends JsonDeserializer<DateTime> {
private static final String DATE_FORMAT = "MM/dd/yyyy";
private DateTimeFormatter formatter = DateTimeFormat.forPattern( DATE_FORMAT );
/**
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
*/
@Override
public DateTime deserialize( JsonParser parser, DeserializationContext ctxt )
throws IOException, JsonProcessingException {
ObjectCodec oc = parser.getCodec();
JsonNode node = oc.readTree( parser );
String dateInStringFormat = node.asText();
DateTime start = formatter.parseDateTime( dateInStringFormat );
return start;
}