I have a json file with a date like this:
{
...
"dob":"20001010",
"registerdate":"20001010121212",
...
}
i am using in class:
public class User implements Serializable {
... some other
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMdd
public java.util.Date dob; // this works
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMddHHmmss")
public java.util.Date registerdate;
}
then in my test i use Gson to convert the file
User user = new Gson().fromJson("user.json", User.class);
but when this run, i get:
com.google.gson.JsonSyntaxException: 20001010121212
I treid with: yyyyMMddHHmmss or YYYYMMddHHmmss or YYYYMMDDHHmmss same results
pom:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
Basically com.google.gson.Gson does not recognize annotation com.fasterxml.jackson.annotation.JsonFormat (two different libs)
Here is another solution using just ObjectMapper from fasterxml lib