How to Serialize Java.util.Date

How to Serialize Java.util.Date

import org.codehaus.jackson.annotate.JsonAutoDetect;
@JsonAutoDetect
@Entity
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:

@JsonSerialize(using=JsonDateSerializer.class)
public Date getDate() {
return date;
}

=============================================

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

==========================

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

Java||Spring-Boot Micro services Application developer||India ||node js || angular