6

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?

6
  • Have you deployed your code in a Cloud environment? Commented Jun 30, 2017 at 14:43
  • its deployed in rehl bare metal server. Commented Jun 30, 2017 at 14:53
  • I wonder what the JPA providers log says? You have looked at it haven't you? ... Commented Jul 1, 2017 at 6:21
  • Haven't tried printing the sql and parameter values. I will try enabling it. This issue is not happening in my dev machine which is running in oracle jdk happening only in the server which is running openjdk. Is there any difference between oracle and open jdk in this regard? Commented Jul 2, 2017 at 2:27
  • I tried printing the hibernate logs the time stamp is correct Commented Jul 4, 2017 at 15:06

1 Answer 1

4

Found the issue, the problem was in jdbc connection URL.

spring.datasource.url=jdbc:mysql://:3307/dbname?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

due to the server time zone setting in the URL, GMT+8 time was converted to UTC which was leading to the 8 hours time difference. After removing the serverTimezone parameter in the URL, date is working fine. Thank you Neil and Shazin for your time and support.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.