Solved] com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String “MM/dd/yyyy”: not a valid representation for date format


InvalidFormatException is subclass of MismatchedInputException. InvalidFormatException occurred because of bad formatting or format not match with the prespecified format of value to deserialize.

InvalidFormatException Example

This example throwing InvalidFormatException because JSONis having the format “MM/dd/yyyy” for dob which is not compatible with standard date format for deserializations. Below is a standard format for a date as JACKSON support:

  • yyyy-MM-dd’T’HH:mm:ss.SSSZ
  • yyyy-MM-dd’T’HH:mm:ss.SSS
  • EEE, dd MMM yyyy HH:mm:ss zzz
  • yyyy-MM-dd
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonFormat;

public class StudentDetail {
	private int rollNumber;
	private String firstName;
	private String lastName;
	private Date dob;
	//getter and setter of class
	}
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJacksonExample2 {

	public static void main(String[] args) {

	    String json="{\"rollNumber\":21 , \"firstName\":\"Saurabh\" , \"lastName\":\"Gupta\",  \"dob\":\"12/13/1985\"}";

	    System.out.println(json);
		try
		{
	    ObjectMapper mapper = new ObjectMapper();
	    StudentDetail student= mapper.readValue(json, StudentDetail.class);
	    System.out.println(student);
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

	}

}

InvalidFormatException Stacktrace


com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "12/13/1985": not a valid representation (error: Failed to parse Date value '12/13/1985': Cannot parse date "12/13/1985": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: (String)"{"rollNumber":21 , "firstName":"Saurabh" , "lastName":"Gupta",  "dob":"12/13/1985"}"; line: 1, column: 71] (through reference chain: com.fiot.json.jackson.exceptions.StudentDetail["dob"])
    at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1549)
    at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:911)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:524)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:467)
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:195)
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:285)
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:268)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)

Solutions

  1. Pass date format for JSON as the standard one for deserializations.
    • yyyy-MM-dd’T’HH:mm:ss.SSSZ
    • yyyy-MM-dd’T’HH:mm:ss.SSS
    • EEE, dd MMM yyyy HH:mm:ss zzz
    • yyyy-MM-dd
  2. Use annotation @JsonFormat as below to support some other new date format.
    public class StudentDetail {
    	private int rollNumber;
    	private String firstName;
    	private String lastName;
    	@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
    	private Date dob;
    	//getter and setter of class
    	}
    

Preferences

https://fasterxml.github.io/jackson-databind/javadoc/2.9/com/fasterxml/jackson/databind/exc/InvalidFormatException.html

You would like to see

Follow below link to learn more on JSON and  JSON issues solutions:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s