0

I have an entity with the field

public class MovieReleaseDateEntity {
     @Basic
     @Column(name = "release_date")
     @Temporal(TemporalType.DATE)
     private Date date;
}

I save the date in the database

https://zapodaj.net/14dfcfb70926a.png.html

I get it after downloading it from the database

"date": "2017-12-06T23:00:00.000+0000"

The date is withdrawn by one day. Why?

3 Answers3

2

tl;dr

These all represent the very same point on the timeline, the very same simultaneous moment:

  • 2017-12-06T23:00:00.000+0000
  • 2017-12-07T00:00:00.000+01:00
  • 2017-12-07T04:30:00.000+05:30

Details

The Europe/Warsaw time zone is currently one hour ahead of UTC.

"2017-12-06T23:00:00.000+0000"

Your string represents a moment in UTC. Note the +0000, an abbreviation of +00:00, which means an offset of zero hours and zero minutes from UTC. So it is at UTC.

That same moment as seen thorough the lens of a clock on the wall on Poland is 00:00 on the morning of the next date. That is the first moment of the new day in Poland.

"2017-12-07T00:00:00.000+01:00"

That same moment is later on a wall-clock in India where the people live five and a half hours ahead of UTC.

"2017-12-07T04:30:00.000+05:30"

If an insomniac in Poland called their friend on the west coast of North America, they might catch their friend still on the job in mid-afternoon at 3 PM on the earlier date. There the clock on the wall is eight hours behind UTC.

"2017-12-06T15:00:00.000-08:00"

Parse your input string as an OffsetDateTime object. To view the same moment in other time zones, adding a ZoneId to get a ZonedDateTime. Search Stack Overflow for details as this has been covered many many times already.

Avoid the troublesome flawed old date-time classes such as Date. Use only their replacements, the modern industry-leading java.time classes.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

It does not revert the day back, it retrieves the date in a different timezone, one hour less and it shows as a day before (so 2017-12-07 00:00:00 becomes 2017-12-06 23:00:00). java.date and java.sql packages that you are probably using are just wrappers around milliseconds represented as Longs so timezone is also needed.

Check your time zone and check the timezone of your server and make sure they match. If you cannot change it for any reason, you can use date formatters to parse the time in a specific timezone when retrieving from the database

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
Urosh T.
  • 3,336
  • 5
  • 34
  • 42
0

I fixed the issue by change the mysql database column type from date to datetime