How to Serialize Java.util.Date

How to Serialize Java.util.Date

import org.codehaus.jackson.annotate.JsonAutoDetect;
public class Student {
private int id;
private double name;
private Date date;

One of the annotations Jackson has is @JsonSerialize.
You basically use this annotation for configuring serialization aspects. In my case,
I decorated by model objects date getter method with this annotation:

public Date getDate() {
return date;


public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat(“MM-dd-yyyy”);
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);


@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “yyyy-MM-dd@HH:mm:ss.SSSZ”)
private Date createdDate;

Java||Spring-Boot|| Micro services Application developer|| angular || INDIA