Home Ask Login Register

Developers Planet

Your answer is one click away!

Raj February 2016

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        

Answers


Raj February 2016

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


Ricard Nàcher Roig February 2016

I know that the question is already answered but I want to share with you an alternative way to solve this without so many lines of code. Just add the @JsonFormat annotation to the pojo property.

@JsonFormat( pattern="MM/dd/YYYY")
public DateTime start;

Post Status

Asked in February 2016
Viewed 1,522 times
Voted 12
Answered 2 times

Search




Leave an answer


Quote of the day: live life