Reputation: 12744
I am litte bit confused. I need to serialize an Object to a json String. I use Jackson
as library.
My Pojo Class Stage
has an attribute fromDate
and its an util.date
.
public class Stage {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy", timezone="UTC+1")
private Date fromDate;
...
Before serialization the fromDate has this value: Wed May 11 00:00:00 CEST 2016
.
My serialize method looks like this:
public static String serialize(Stages stages) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(stages);
return s;
}
But after serialize my Object the dateFrom in the json String hast this value:
..."fromDate":"10.05.2016"...
. So the Date is wrong.
I used the pattern of @JsonFormat
and tried it also with the ObjectMapper
configuration.
public static String serialize(Stages stages) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
SimpleDateFormat dateFormat = new SimpleDateFormat(Util.SDF_dd_mm_yyyy);
objectMapper.setDateFormat(dateFormat);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC+1"));
String s = objectMapper.writeValueAsString(stages);
return s;
}
But the date is not my expected one: ..."fromDate":"11.05.2016"...
What do I wrong?
Upvotes: 0
Views: 1649
Reputation: 3176
I think this is just another timezone issue. The serialized date looks like it's in UTC (?), so it's actually correct that you're seeing 10.05, cause 2016-11-05 00:00 UTC+1 is really 2016-10-05 23:00 UTC..
You should probably try adapting the code where you're reading this serialized value to take into account what timezone the date has been serialized in.
Upvotes: 1