lesnar
lesnar

Reputation: 2480

Custom Date deserializing: Jackson

i am trying to use the custom format for my date like this :

public class CustomDateMappingDeserialize extends JsonDeserializer<Date>{

    @Override
    public Date deserialize(JsonParser paramJsonParser, DeserializationContext paramDeserializationContext) throws IOException, JsonProcessingException {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date = paramJsonParser.getText();
            try {
                Date formattedDate= format.parse(date);
              return formattedDate;
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

    }

}

Date is 2011-04-08 09:00:00 and after parsing i am getting the same date in FormattedDate and no exception.

is there something i am missing ?

Thanks

Upvotes: 0

Views: 2449

Answers (1)

Nico
Nico

Reputation: 1813

The thing is that the Object Date will always append a Time so if you want to cut it out you can convert it to a string like this:

String input = paramJsonParser.getText();
DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // this has to be like your input
Date date = inputFormatter.parse(input);

DateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd");
String output = outputFormatter.format(date); // Output : yyyy-MM-dd

Upvotes: 2

Related Questions