I have a problem with displaying dates in JSON output. In code I use java.util.Date
and its value is 2019-03-07
but in JSON I got 2019-03-06 23:00:00
. I think the problem is in timezone, but I don't use timezones in DB and in code too.
I was trying to fix it with
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone="UTC")
and
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone="Europe/Warsaw")
The first didn't help, the second helped but I don't accept this solution.
Part of my controller:
return new ThisDay(
sysoperMgr.getToday(),
new Date()
);
This is object which I return.
@Getter
@Setter
public class ThisDay {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
Date dataZamkniecia;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
Date dataSystemowa;
public BiezacaDoba(Date dataZamkniecia, Date dataSystemowa) {
this.dataZamkniecia = dataZamkniecia; // cdate = 2019-03-07T00:00:00.000+0100
this.dataSystemowa = dataSystemowa; // cdate = 2019-03-27T16:08:12.343+0100
}
}
This function gets date:
public Date getToday() {
Timestamp timestamp = sysoperDao.getDataOstatniejZamknietejDoby(); // cdate = 2019-03-06T00:00:00.000+0100
java.util.Date lastDay = new java.sql.Date(misc.roundTimestamp(timestamp).getTime()); // cdate = 2019-03-06T00:00:00.000+0100
java.util.Date thisDay = misc.incrementDate(ostatniaDoba, Increment.DAILY, 1); // cdate = 2019-03-07T00:00:00.000+0100
return thisDay;
}
Json result:
{
"dataZamkniecia":"2019-03-06 23:00:00",
"dataSystemowa": "2019-03-27 15:12:15"
}
How do yo get the JSON to display the date always in the local timezone?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…