Wednesday, 7 April 2021

@JsonFormat on Getter

 @Jsonformat is an annotation belong to package Jackson-databind

It is used on a Getter method to format how the Date instance is serialized.  By default, a Date instance is serialized as a number ref. to the begging of the time counting. 

As using @JsonFormat, the time output is formatted ref. to a default time-zone or a locale. Normally it is the GMT time zone.  


@Getter
@Setter
@AllArgsConstructor
@Builder
public class RoomReservation {
private Long roomId;
private Long guestId;
private String roomName;
private String roomNumber;
private String firstName;
private String lastName;

@Getter(AccessLevel.NONE)
private Date date;

@JsonFormat(pattern = "yyyy-MM-dd", shape = JsonFormat.Shape.STRING, timezone = "Europe/Copenhagen")
public Date getDate() {
return date;
}

}





No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...