Date format Mapping to JSON Jackson
You can use the @JsonFormat
annotation to specify the format of date fields in a JSON payload when serializing or deserializing using Jackson.
Here is an example of how you can use @JsonFormat
to specify the format of a java.util.Date
field:
import com.fasterxml.jackson.annotation.JsonFormat;
public class MyClass {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date date;
}
This will format the date
field in the JSON payload as a string in the format "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
, which is a common format used for dates in JSON.
You can also use @JsonFormat
to specify the time zone for the date field, using the timezone
attribute.
import com.fasterxml.jackson.annotation.JsonFormat;
public class MyClass {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone = "UTC")
private Date date;
}
This will format the date
field in the JSON payload as a string in the specified format, in the UTC time zone.