Spring JPA java.util.Date to Mysql timestamp with wrong date value

Viewed 4227

I have a JPA entity with a date field is persisted into Mysql DateTime.

Field:

  @Column(name = "CREATION_DATE")
  private Date creationDate;

Column:
CREATION_DATE datetime DEFAULT NULL,

Setter:

request.setCreationDate(new Date());//Value set to current date

I am getting an strange error that persisted date is 8 hours lesser than the current time. Tried few approaches as below:

  1. Using @Temporal(TemporalType.TIMESTAMP) for the field

  2. Setting default TimeZone
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));

  3. Printing the date before setting, which displays the correct value.

  4. Setting the timezone at mysql layer as well:
    SET GLOBAL time_zone = '+8:00'; SET SESSION time_zone = '+8:00';

  5. Changing the field type to java.sql.Timestamp

none of the above approaches helped. Anyone had a similar issue? Any clue?

1 Answers
Related