简体   繁体   中英

Java - date, format, time zones and Spring Boot defaults

I have simple Java object with a date field:

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date date;

When I investigate the date with debugger I see:

Wed Jun 14 00:00:00 BST 2017

But once I return it with Spring boot controller I get:

"date": "2017-06-13 23:00:00"
  1. What's causing the difference?
  2. Why Java treats the date as BST ?
  3. Does Java Date class contain time-zone information or just plain timestamp in long format?
  4. Is Spring boot using UTC format by default while serialising DTOs to JSON?

java.util.Date has no timezone information (only the long timestamp), but it uses the system's default timezone in the toString() method - you can find more info about this here (as already suggested in the comments).

Just check the value of TimeZone.getDefault() . It'll probably be Europe/London - as London in now in summer time, the short name (used by Date.toString() ) of this timezone is BST .

As your output suggests, Spring is probably using UTC (as 2017-06-13 23:00:00 in UTC is 2017-06-14 00:00:00 in BST ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM