Search code examples
javajacksonjodatimefasterxml

Converting date in String format to Joda DateTime using FasterXML


I am trying to do a simple exercise of converting String into Joda DateTime using Faster XML (Jackson) https://github.com/FasterXML/jackson

I am able to convert it if the String format is like "1972-12-28T12:00:01.000Z". But as soon as I convert the format to simple "09/23/2016" (MM/dd/yyyy) the code fails. Following is my code:

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;
  }
}

The exception is as follows:

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

This looks so simple but I am not sure why there is an issue. I can't change the date format to anything but "MM/dd/yyyy". I used Java Date and it works but I am not able to persist the data into database since the columns are mapped as Joda LocalDate in the entity object and when I convert Date to LocalDate I am having persistent issue. But lets not go there. If someone can help me with my above problem I will appreciate it a lot.


Solution

  • Finally I got this working. I used the custom deserializer to get this working. Following is the code:

    Bean in question

    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;
    }
    }
    

    The Adapter:

    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;
    }