Mismatch between DATETIME values in H2 and MySQL databases inserted from Java/KotlinSet default timezone H2 databaseConvert from MySQL datetime to another format with PHPHow do you set a default value for a MySQL Datetime column?How to get an enum value from a string value in Java?PHP date() format when inserting into datetime in MySQLConnect Java to a MySQL databaseIgnoring time zones altogether in Rails and PostgreSQLMysql between two datetime columnsDjango - MySQL database datetime field gets clearedHibernate ignoring property values from MappedSuperClasses?PHP datetime returned from mysql is wrong

Is my sink P-trap too low?

Plot irregular circle in latex

What is this WWII four-engine plane on skis?

Transit visa to Hong Kong

Why cannot a convert make certain statements? I feel they are being pushed away at the same time respect is being given to them

Can an infinite series be thought of as adding up "infinitely many" terms?

Madrid to London w/ Expired 90/180 days stay as US citizen

Are all men created equal according to Hinduism? Is this predominant western belief in agreement with the Vedas?

Amortized Loans seem to benefit the bank more than the customer

Are there any “Third Order” acronyms used in space exploration?

In what sequence should an advanced civilization teach technology to medieval society to maximize rate of adoption?

What does the Free Recovery sign (UK) actually mean?

Does Forgotten Realms setting count as “High magic”?

Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?

Output Distinct Factor Cuboids

How would you translate Evangelii Nuntiandi?

Is it appropriate to CC a lot of people on an email?

Why does '/' contain '..'?

Neta Revai is achzareyos?

What's the benefit of prohibiting the use of techniques/language constructs that have not been taught?

Did Sauron ever betray Morgoth?

How many people need to succeed in a group check with three people?

How to draw a Venn diagram for X - (Y intersect Z)?

Test to know when to use GLM over Linear Regression?



Mismatch between DATETIME values in H2 and MySQL databases inserted from Java/Kotlin


Set default timezone H2 databaseConvert from MySQL datetime to another format with PHPHow do you set a default value for a MySQL Datetime column?How to get an enum value from a string value in Java?PHP date() format when inserting into datetime in MySQLConnect Java to a MySQL databaseIgnoring time zones altogether in Rails and PostgreSQLMysql between two datetime columnsDjango - MySQL database datetime field gets clearedHibernate ignoring property values from MappedSuperClasses?PHP datetime returned from mysql is wrong






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















TLDR:



How to always save correct UTC date time value into the field of DATETIME type of both H2 and MySQL databases with Java Hibernate?



Full context:



I have a table with DATETIME field in the database and I want to insert rows where:



  • by default (when no value is given) will be stored current UTC time

  • or if the UTC date time is given, it should be stored without
    additional timezone conversions.

The problem that it has to run on local H2 database as well as on local mysql inside Docker and on external AWS RDS MySQL instance.



And I'm having a hard time making datetime to be saved correctly in all 3 instances.



So far it's either local and aws mysql instances are getting correct values but local H2 gets wrong value, or other way around, when local H2 gets correct value but MySQL instances are getting wrong values.



Here are shortened snippets of kotlin code that I have.



Code that works for H2 but doesn't work for MySQL in Docker and AWS:



@Entity
data class SomeEntity(
val createdAt: LocalDateTime = LocalDateTime.now(Clock.systemUTC())
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and H2 will get correct value of '2019-03-28 12:36:56',
// but it will be wrong for MySQL, it will get '2019-03-28 11:36:56'
)

val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter)
// In this case when createdAt is explicitly given when saving new entry in db,
// H2 gets correct value '2012-11-30 16:13:21',
// but MySQL DBs will get wrong value of '2012-11-30 17:13:21'


Code that works for MySQL in Docker and AWS but doesn't work for H2:



@Entity
data class SomeEntity(
val createdAt: Date = Date()
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and MySQL DBs will get correct value of '2019-03-28 12:36:56'
// but it will be wrong for H2 as it will get '2019-03-28 13:36:56' (my current local time instead of UTC)
)

val dateTimeFormatter = SimpleDateFormat("yyyy-MM-dd H:mm:ss")
dateTimeFormatter.timeZone = TimeZone.getTimeZone("UTC")

createdAt = dateTimeFormatter.parse("2012-11-30 16:13:21")
// In this case when createdAt is explicitly given when saving new entry in db,
// MySQL DBs will get correct value '2012-11-30 16:13:21',
// but H2 will get wrong value of '2012-11-30 17:13:21'


This runs on: Spring Boot 2.1.3, Hibernate Core 5.3.7, MySQL 8.0.13, H2 1.4.197



I've seen bunch of questions online and also on stackoverflow but unfortunately none of the solutions could fix my problem.



Update



After additional debugging with multiple approaches, looking through the logs of Hibernate, H2 and MySQL, it looks like UTC time is treated exactly opposite way between H2 and MySQL.



Saving to local H2:



  • [wrong] using Date, when UTC is 09:55, Hibernate logs value "Fri Mar 29 10:55:09 CET 2019", it's saved as "2019-03-29 10:55:09.412".

  • [wrong] using Instant, when UTC is 16:48, Hibernate logs value "2019-03-28T16:48:18.270Z", it's saved as "2019-03-28 17:48:18.27".

  • [wrong] using OffsetDateTime, when UTC is 10:11, Hibernate logs value "2019-03-29T10:11:30.672Z", it's saved as "2019-03-29 11:11:30.672".

  • [correct] using LocalDateTime, when UTC is 16:50, Hibernate logs value "2019-03-28T16:50:20.697", it's saved as "2019-03-28 16:50:20.697".

Saving to MySQL in local docker:



  • [correct] using Date, when UTC is 09:51, Hibernate logs value "Fri Mar 29 10:51:56 CET 2019", it's saved as "2019-03-29 09:51:56.519".

  • [correct] using Instant, when UTC is 09:38, Hibernate logs value "2019-03-29T09:38:59.172Z", it's saved as "2019-03-29 09:38:59.172".

  • [correct] using OffsetDateTime, when UTC is 10:14, Hibernate logs value "2019-03-29T10:14:22.658Z", it's saved as "2019-03-29 10:14:22.658".

  • [wrong] using LocalDateTime, when UTC is 16:57, Hibernate logs value "2019-03-28T16:57:35.631", it's saved as "2019-03-28 15:57:35.631".









share|improve this question





















  • 1





    Compare it with SQL CURRENT_TIMESTAMP() (h2+mysql) to see whether the times follow the same clock.

    – Joop Eggen
    Mar 28 at 13:22











  • @joop-eggen hm, timestamp differs. On local H2 the CURRENT_TIMESTAMP is 2019-03-28 16:04:29.434 and on both local Docker and AWS RDS MySQL nodes the CURRENT_TIMESTAMP is 2019-03-28 15:04:29. For the reference, the current local time is 2019-03-28 16:04:29 and current UTC time: 2019-03-28 15:04:29. I'm not sure if that's the origin of the problem, and if yes, is there a way to fix that?

    – WallTearer
    Mar 28 at 15:10






  • 1





    So one server runs on UTC, one on local time. I did find stackoverflow.com/questions/13176251/… keywords: database time zone. BTW the new java time classes have my preference.

    – Joop Eggen
    Mar 28 at 15:20












  • @joop-eggen yep, I stumbled across that question multiple times, but setting UTC Timezone for JVM didn't fix the issue. Anyways, that felt like quite a dirty approach.

    – WallTearer
    Mar 28 at 15:46











  • One would like to be able to use CURRENT_TIMESTAMP() almost equal to now(), so the databases should be configurable. MySQL maybe. Please should you find something, answer yourself; as that is worthwile.

    – Joop Eggen
    Mar 28 at 15:51

















1















TLDR:



How to always save correct UTC date time value into the field of DATETIME type of both H2 and MySQL databases with Java Hibernate?



Full context:



I have a table with DATETIME field in the database and I want to insert rows where:



  • by default (when no value is given) will be stored current UTC time

  • or if the UTC date time is given, it should be stored without
    additional timezone conversions.

The problem that it has to run on local H2 database as well as on local mysql inside Docker and on external AWS RDS MySQL instance.



And I'm having a hard time making datetime to be saved correctly in all 3 instances.



So far it's either local and aws mysql instances are getting correct values but local H2 gets wrong value, or other way around, when local H2 gets correct value but MySQL instances are getting wrong values.



Here are shortened snippets of kotlin code that I have.



Code that works for H2 but doesn't work for MySQL in Docker and AWS:



@Entity
data class SomeEntity(
val createdAt: LocalDateTime = LocalDateTime.now(Clock.systemUTC())
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and H2 will get correct value of '2019-03-28 12:36:56',
// but it will be wrong for MySQL, it will get '2019-03-28 11:36:56'
)

val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter)
// In this case when createdAt is explicitly given when saving new entry in db,
// H2 gets correct value '2012-11-30 16:13:21',
// but MySQL DBs will get wrong value of '2012-11-30 17:13:21'


Code that works for MySQL in Docker and AWS but doesn't work for H2:



@Entity
data class SomeEntity(
val createdAt: Date = Date()
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and MySQL DBs will get correct value of '2019-03-28 12:36:56'
// but it will be wrong for H2 as it will get '2019-03-28 13:36:56' (my current local time instead of UTC)
)

val dateTimeFormatter = SimpleDateFormat("yyyy-MM-dd H:mm:ss")
dateTimeFormatter.timeZone = TimeZone.getTimeZone("UTC")

createdAt = dateTimeFormatter.parse("2012-11-30 16:13:21")
// In this case when createdAt is explicitly given when saving new entry in db,
// MySQL DBs will get correct value '2012-11-30 16:13:21',
// but H2 will get wrong value of '2012-11-30 17:13:21'


This runs on: Spring Boot 2.1.3, Hibernate Core 5.3.7, MySQL 8.0.13, H2 1.4.197



I've seen bunch of questions online and also on stackoverflow but unfortunately none of the solutions could fix my problem.



Update



After additional debugging with multiple approaches, looking through the logs of Hibernate, H2 and MySQL, it looks like UTC time is treated exactly opposite way between H2 and MySQL.



Saving to local H2:



  • [wrong] using Date, when UTC is 09:55, Hibernate logs value "Fri Mar 29 10:55:09 CET 2019", it's saved as "2019-03-29 10:55:09.412".

  • [wrong] using Instant, when UTC is 16:48, Hibernate logs value "2019-03-28T16:48:18.270Z", it's saved as "2019-03-28 17:48:18.27".

  • [wrong] using OffsetDateTime, when UTC is 10:11, Hibernate logs value "2019-03-29T10:11:30.672Z", it's saved as "2019-03-29 11:11:30.672".

  • [correct] using LocalDateTime, when UTC is 16:50, Hibernate logs value "2019-03-28T16:50:20.697", it's saved as "2019-03-28 16:50:20.697".

Saving to MySQL in local docker:



  • [correct] using Date, when UTC is 09:51, Hibernate logs value "Fri Mar 29 10:51:56 CET 2019", it's saved as "2019-03-29 09:51:56.519".

  • [correct] using Instant, when UTC is 09:38, Hibernate logs value "2019-03-29T09:38:59.172Z", it's saved as "2019-03-29 09:38:59.172".

  • [correct] using OffsetDateTime, when UTC is 10:14, Hibernate logs value "2019-03-29T10:14:22.658Z", it's saved as "2019-03-29 10:14:22.658".

  • [wrong] using LocalDateTime, when UTC is 16:57, Hibernate logs value "2019-03-28T16:57:35.631", it's saved as "2019-03-28 15:57:35.631".









share|improve this question





















  • 1





    Compare it with SQL CURRENT_TIMESTAMP() (h2+mysql) to see whether the times follow the same clock.

    – Joop Eggen
    Mar 28 at 13:22











  • @joop-eggen hm, timestamp differs. On local H2 the CURRENT_TIMESTAMP is 2019-03-28 16:04:29.434 and on both local Docker and AWS RDS MySQL nodes the CURRENT_TIMESTAMP is 2019-03-28 15:04:29. For the reference, the current local time is 2019-03-28 16:04:29 and current UTC time: 2019-03-28 15:04:29. I'm not sure if that's the origin of the problem, and if yes, is there a way to fix that?

    – WallTearer
    Mar 28 at 15:10






  • 1





    So one server runs on UTC, one on local time. I did find stackoverflow.com/questions/13176251/… keywords: database time zone. BTW the new java time classes have my preference.

    – Joop Eggen
    Mar 28 at 15:20












  • @joop-eggen yep, I stumbled across that question multiple times, but setting UTC Timezone for JVM didn't fix the issue. Anyways, that felt like quite a dirty approach.

    – WallTearer
    Mar 28 at 15:46











  • One would like to be able to use CURRENT_TIMESTAMP() almost equal to now(), so the databases should be configurable. MySQL maybe. Please should you find something, answer yourself; as that is worthwile.

    – Joop Eggen
    Mar 28 at 15:51













1












1








1








TLDR:



How to always save correct UTC date time value into the field of DATETIME type of both H2 and MySQL databases with Java Hibernate?



Full context:



I have a table with DATETIME field in the database and I want to insert rows where:



  • by default (when no value is given) will be stored current UTC time

  • or if the UTC date time is given, it should be stored without
    additional timezone conversions.

The problem that it has to run on local H2 database as well as on local mysql inside Docker and on external AWS RDS MySQL instance.



And I'm having a hard time making datetime to be saved correctly in all 3 instances.



So far it's either local and aws mysql instances are getting correct values but local H2 gets wrong value, or other way around, when local H2 gets correct value but MySQL instances are getting wrong values.



Here are shortened snippets of kotlin code that I have.



Code that works for H2 but doesn't work for MySQL in Docker and AWS:



@Entity
data class SomeEntity(
val createdAt: LocalDateTime = LocalDateTime.now(Clock.systemUTC())
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and H2 will get correct value of '2019-03-28 12:36:56',
// but it will be wrong for MySQL, it will get '2019-03-28 11:36:56'
)

val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter)
// In this case when createdAt is explicitly given when saving new entry in db,
// H2 gets correct value '2012-11-30 16:13:21',
// but MySQL DBs will get wrong value of '2012-11-30 17:13:21'


Code that works for MySQL in Docker and AWS but doesn't work for H2:



@Entity
data class SomeEntity(
val createdAt: Date = Date()
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and MySQL DBs will get correct value of '2019-03-28 12:36:56'
// but it will be wrong for H2 as it will get '2019-03-28 13:36:56' (my current local time instead of UTC)
)

val dateTimeFormatter = SimpleDateFormat("yyyy-MM-dd H:mm:ss")
dateTimeFormatter.timeZone = TimeZone.getTimeZone("UTC")

createdAt = dateTimeFormatter.parse("2012-11-30 16:13:21")
// In this case when createdAt is explicitly given when saving new entry in db,
// MySQL DBs will get correct value '2012-11-30 16:13:21',
// but H2 will get wrong value of '2012-11-30 17:13:21'


This runs on: Spring Boot 2.1.3, Hibernate Core 5.3.7, MySQL 8.0.13, H2 1.4.197



I've seen bunch of questions online and also on stackoverflow but unfortunately none of the solutions could fix my problem.



Update



After additional debugging with multiple approaches, looking through the logs of Hibernate, H2 and MySQL, it looks like UTC time is treated exactly opposite way between H2 and MySQL.



Saving to local H2:



  • [wrong] using Date, when UTC is 09:55, Hibernate logs value "Fri Mar 29 10:55:09 CET 2019", it's saved as "2019-03-29 10:55:09.412".

  • [wrong] using Instant, when UTC is 16:48, Hibernate logs value "2019-03-28T16:48:18.270Z", it's saved as "2019-03-28 17:48:18.27".

  • [wrong] using OffsetDateTime, when UTC is 10:11, Hibernate logs value "2019-03-29T10:11:30.672Z", it's saved as "2019-03-29 11:11:30.672".

  • [correct] using LocalDateTime, when UTC is 16:50, Hibernate logs value "2019-03-28T16:50:20.697", it's saved as "2019-03-28 16:50:20.697".

Saving to MySQL in local docker:



  • [correct] using Date, when UTC is 09:51, Hibernate logs value "Fri Mar 29 10:51:56 CET 2019", it's saved as "2019-03-29 09:51:56.519".

  • [correct] using Instant, when UTC is 09:38, Hibernate logs value "2019-03-29T09:38:59.172Z", it's saved as "2019-03-29 09:38:59.172".

  • [correct] using OffsetDateTime, when UTC is 10:14, Hibernate logs value "2019-03-29T10:14:22.658Z", it's saved as "2019-03-29 10:14:22.658".

  • [wrong] using LocalDateTime, when UTC is 16:57, Hibernate logs value "2019-03-28T16:57:35.631", it's saved as "2019-03-28 15:57:35.631".









share|improve this question
















TLDR:



How to always save correct UTC date time value into the field of DATETIME type of both H2 and MySQL databases with Java Hibernate?



Full context:



I have a table with DATETIME field in the database and I want to insert rows where:



  • by default (when no value is given) will be stored current UTC time

  • or if the UTC date time is given, it should be stored without
    additional timezone conversions.

The problem that it has to run on local H2 database as well as on local mysql inside Docker and on external AWS RDS MySQL instance.



And I'm having a hard time making datetime to be saved correctly in all 3 instances.



So far it's either local and aws mysql instances are getting correct values but local H2 gets wrong value, or other way around, when local H2 gets correct value but MySQL instances are getting wrong values.



Here are shortened snippets of kotlin code that I have.



Code that works for H2 but doesn't work for MySQL in Docker and AWS:



@Entity
data class SomeEntity(
val createdAt: LocalDateTime = LocalDateTime.now(Clock.systemUTC())
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and H2 will get correct value of '2019-03-28 12:36:56',
// but it will be wrong for MySQL, it will get '2019-03-28 11:36:56'
)

val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter)
// In this case when createdAt is explicitly given when saving new entry in db,
// H2 gets correct value '2012-11-30 16:13:21',
// but MySQL DBs will get wrong value of '2012-11-30 17:13:21'


Code that works for MySQL in Docker and AWS but doesn't work for H2:



@Entity
data class SomeEntity(
val createdAt: Date = Date()
// If createdAt is not explicitly given when saving new entry in db, the default value will be used
// and MySQL DBs will get correct value of '2019-03-28 12:36:56'
// but it will be wrong for H2 as it will get '2019-03-28 13:36:56' (my current local time instead of UTC)
)

val dateTimeFormatter = SimpleDateFormat("yyyy-MM-dd H:mm:ss")
dateTimeFormatter.timeZone = TimeZone.getTimeZone("UTC")

createdAt = dateTimeFormatter.parse("2012-11-30 16:13:21")
// In this case when createdAt is explicitly given when saving new entry in db,
// MySQL DBs will get correct value '2012-11-30 16:13:21',
// but H2 will get wrong value of '2012-11-30 17:13:21'


This runs on: Spring Boot 2.1.3, Hibernate Core 5.3.7, MySQL 8.0.13, H2 1.4.197



I've seen bunch of questions online and also on stackoverflow but unfortunately none of the solutions could fix my problem.



Update



After additional debugging with multiple approaches, looking through the logs of Hibernate, H2 and MySQL, it looks like UTC time is treated exactly opposite way between H2 and MySQL.



Saving to local H2:



  • [wrong] using Date, when UTC is 09:55, Hibernate logs value "Fri Mar 29 10:55:09 CET 2019", it's saved as "2019-03-29 10:55:09.412".

  • [wrong] using Instant, when UTC is 16:48, Hibernate logs value "2019-03-28T16:48:18.270Z", it's saved as "2019-03-28 17:48:18.27".

  • [wrong] using OffsetDateTime, when UTC is 10:11, Hibernate logs value "2019-03-29T10:11:30.672Z", it's saved as "2019-03-29 11:11:30.672".

  • [correct] using LocalDateTime, when UTC is 16:50, Hibernate logs value "2019-03-28T16:50:20.697", it's saved as "2019-03-28 16:50:20.697".

Saving to MySQL in local docker:



  • [correct] using Date, when UTC is 09:51, Hibernate logs value "Fri Mar 29 10:51:56 CET 2019", it's saved as "2019-03-29 09:51:56.519".

  • [correct] using Instant, when UTC is 09:38, Hibernate logs value "2019-03-29T09:38:59.172Z", it's saved as "2019-03-29 09:38:59.172".

  • [correct] using OffsetDateTime, when UTC is 10:14, Hibernate logs value "2019-03-29T10:14:22.658Z", it's saved as "2019-03-29 10:14:22.658".

  • [wrong] using LocalDateTime, when UTC is 16:57, Hibernate logs value "2019-03-28T16:57:35.631", it's saved as "2019-03-28 15:57:35.631".






java mysql datetime kotlin h2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 11:18







WallTearer

















asked Mar 28 at 12:59









WallTearerWallTearer

1,3651 gold badge20 silver badges26 bronze badges




1,3651 gold badge20 silver badges26 bronze badges










  • 1





    Compare it with SQL CURRENT_TIMESTAMP() (h2+mysql) to see whether the times follow the same clock.

    – Joop Eggen
    Mar 28 at 13:22











  • @joop-eggen hm, timestamp differs. On local H2 the CURRENT_TIMESTAMP is 2019-03-28 16:04:29.434 and on both local Docker and AWS RDS MySQL nodes the CURRENT_TIMESTAMP is 2019-03-28 15:04:29. For the reference, the current local time is 2019-03-28 16:04:29 and current UTC time: 2019-03-28 15:04:29. I'm not sure if that's the origin of the problem, and if yes, is there a way to fix that?

    – WallTearer
    Mar 28 at 15:10






  • 1





    So one server runs on UTC, one on local time. I did find stackoverflow.com/questions/13176251/… keywords: database time zone. BTW the new java time classes have my preference.

    – Joop Eggen
    Mar 28 at 15:20












  • @joop-eggen yep, I stumbled across that question multiple times, but setting UTC Timezone for JVM didn't fix the issue. Anyways, that felt like quite a dirty approach.

    – WallTearer
    Mar 28 at 15:46











  • One would like to be able to use CURRENT_TIMESTAMP() almost equal to now(), so the databases should be configurable. MySQL maybe. Please should you find something, answer yourself; as that is worthwile.

    – Joop Eggen
    Mar 28 at 15:51












  • 1





    Compare it with SQL CURRENT_TIMESTAMP() (h2+mysql) to see whether the times follow the same clock.

    – Joop Eggen
    Mar 28 at 13:22











  • @joop-eggen hm, timestamp differs. On local H2 the CURRENT_TIMESTAMP is 2019-03-28 16:04:29.434 and on both local Docker and AWS RDS MySQL nodes the CURRENT_TIMESTAMP is 2019-03-28 15:04:29. For the reference, the current local time is 2019-03-28 16:04:29 and current UTC time: 2019-03-28 15:04:29. I'm not sure if that's the origin of the problem, and if yes, is there a way to fix that?

    – WallTearer
    Mar 28 at 15:10






  • 1





    So one server runs on UTC, one on local time. I did find stackoverflow.com/questions/13176251/… keywords: database time zone. BTW the new java time classes have my preference.

    – Joop Eggen
    Mar 28 at 15:20












  • @joop-eggen yep, I stumbled across that question multiple times, but setting UTC Timezone for JVM didn't fix the issue. Anyways, that felt like quite a dirty approach.

    – WallTearer
    Mar 28 at 15:46











  • One would like to be able to use CURRENT_TIMESTAMP() almost equal to now(), so the databases should be configurable. MySQL maybe. Please should you find something, answer yourself; as that is worthwile.

    – Joop Eggen
    Mar 28 at 15:51







1




1





Compare it with SQL CURRENT_TIMESTAMP() (h2+mysql) to see whether the times follow the same clock.

– Joop Eggen
Mar 28 at 13:22





Compare it with SQL CURRENT_TIMESTAMP() (h2+mysql) to see whether the times follow the same clock.

– Joop Eggen
Mar 28 at 13:22













@joop-eggen hm, timestamp differs. On local H2 the CURRENT_TIMESTAMP is 2019-03-28 16:04:29.434 and on both local Docker and AWS RDS MySQL nodes the CURRENT_TIMESTAMP is 2019-03-28 15:04:29. For the reference, the current local time is 2019-03-28 16:04:29 and current UTC time: 2019-03-28 15:04:29. I'm not sure if that's the origin of the problem, and if yes, is there a way to fix that?

– WallTearer
Mar 28 at 15:10





@joop-eggen hm, timestamp differs. On local H2 the CURRENT_TIMESTAMP is 2019-03-28 16:04:29.434 and on both local Docker and AWS RDS MySQL nodes the CURRENT_TIMESTAMP is 2019-03-28 15:04:29. For the reference, the current local time is 2019-03-28 16:04:29 and current UTC time: 2019-03-28 15:04:29. I'm not sure if that's the origin of the problem, and if yes, is there a way to fix that?

– WallTearer
Mar 28 at 15:10




1




1





So one server runs on UTC, one on local time. I did find stackoverflow.com/questions/13176251/… keywords: database time zone. BTW the new java time classes have my preference.

– Joop Eggen
Mar 28 at 15:20






So one server runs on UTC, one on local time. I did find stackoverflow.com/questions/13176251/… keywords: database time zone. BTW the new java time classes have my preference.

– Joop Eggen
Mar 28 at 15:20














@joop-eggen yep, I stumbled across that question multiple times, but setting UTC Timezone for JVM didn't fix the issue. Anyways, that felt like quite a dirty approach.

– WallTearer
Mar 28 at 15:46





@joop-eggen yep, I stumbled across that question multiple times, but setting UTC Timezone for JVM didn't fix the issue. Anyways, that felt like quite a dirty approach.

– WallTearer
Mar 28 at 15:46













One would like to be able to use CURRENT_TIMESTAMP() almost equal to now(), so the databases should be configurable. MySQL maybe. Please should you find something, answer yourself; as that is worthwile.

– Joop Eggen
Mar 28 at 15:51





One would like to be able to use CURRENT_TIMESTAMP() almost equal to now(), so the databases should be configurable. MySQL maybe. Please should you find something, answer yourself; as that is worthwile.

– Joop Eggen
Mar 28 at 15:51












1 Answer
1






active

oldest

votes


















0
















So looks like the fix was to set UTC timezone for the JDBC connection (instead of JVM):



spring.jpa.properties.hibernate.jdbc.time_zone=UTC


and it relies on using Instant for keeping the value on Java side and with created_at field having DATETIME type in MySQL and H2.



The shortened resulting kotlin code is:



@Entity
data class SomeEntity(
val createdAt: Instant = Instant.now() // default created date is current UTC time
)

val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter).toInstant(ZoneOffset.UTC)


Ideas taken from comments of "Joop Eggen", this and this article.



Bonus



I guess if you're reading this, you might also need help with debugging SQL queries.



1. To print SQL queries running on H2 add TRACE_LEVEL_FILE=2 and TRACE_LEVEL_SYSTEM_OUT=2 to connection string (see here):



spring.datasource.url=jdbc:h2:mem:dbname;TRACE_LEVEL_FILE=2;TRACE_LEVEL_SYSTEM_OUT=2;


2. To enable hibernate logs:



spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=TRACE


3. To enable query logs in MySQL (one of the approaches, don't use on production db!):



SET GLOBAL general_log = 'ON';
SET global log_output = 'table';
select * from mysql.general_log ORDER BY event_time DESC;





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55398279%2fmismatch-between-datetime-values-in-h2-and-mysql-databases-inserted-from-java-ko%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    So looks like the fix was to set UTC timezone for the JDBC connection (instead of JVM):



    spring.jpa.properties.hibernate.jdbc.time_zone=UTC


    and it relies on using Instant for keeping the value on Java side and with created_at field having DATETIME type in MySQL and H2.



    The shortened resulting kotlin code is:



    @Entity
    data class SomeEntity(
    val createdAt: Instant = Instant.now() // default created date is current UTC time
    )

    val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

    createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter).toInstant(ZoneOffset.UTC)


    Ideas taken from comments of "Joop Eggen", this and this article.



    Bonus



    I guess if you're reading this, you might also need help with debugging SQL queries.



    1. To print SQL queries running on H2 add TRACE_LEVEL_FILE=2 and TRACE_LEVEL_SYSTEM_OUT=2 to connection string (see here):



    spring.datasource.url=jdbc:h2:mem:dbname;TRACE_LEVEL_FILE=2;TRACE_LEVEL_SYSTEM_OUT=2;


    2. To enable hibernate logs:



    spring.jpa.properties.hibernate.show_sql=true
    spring.jpa.properties.hibernate.use_sql_comments=true
    spring.jpa.properties.hibernate.format_sql=true
    logging.level.org.hibernate.type=TRACE


    3. To enable query logs in MySQL (one of the approaches, don't use on production db!):



    SET GLOBAL general_log = 'ON';
    SET global log_output = 'table';
    select * from mysql.general_log ORDER BY event_time DESC;





    share|improve this answer





























      0
















      So looks like the fix was to set UTC timezone for the JDBC connection (instead of JVM):



      spring.jpa.properties.hibernate.jdbc.time_zone=UTC


      and it relies on using Instant for keeping the value on Java side and with created_at field having DATETIME type in MySQL and H2.



      The shortened resulting kotlin code is:



      @Entity
      data class SomeEntity(
      val createdAt: Instant = Instant.now() // default created date is current UTC time
      )

      val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

      createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter).toInstant(ZoneOffset.UTC)


      Ideas taken from comments of "Joop Eggen", this and this article.



      Bonus



      I guess if you're reading this, you might also need help with debugging SQL queries.



      1. To print SQL queries running on H2 add TRACE_LEVEL_FILE=2 and TRACE_LEVEL_SYSTEM_OUT=2 to connection string (see here):



      spring.datasource.url=jdbc:h2:mem:dbname;TRACE_LEVEL_FILE=2;TRACE_LEVEL_SYSTEM_OUT=2;


      2. To enable hibernate logs:



      spring.jpa.properties.hibernate.show_sql=true
      spring.jpa.properties.hibernate.use_sql_comments=true
      spring.jpa.properties.hibernate.format_sql=true
      logging.level.org.hibernate.type=TRACE


      3. To enable query logs in MySQL (one of the approaches, don't use on production db!):



      SET GLOBAL general_log = 'ON';
      SET global log_output = 'table';
      select * from mysql.general_log ORDER BY event_time DESC;





      share|improve this answer



























        0














        0










        0









        So looks like the fix was to set UTC timezone for the JDBC connection (instead of JVM):



        spring.jpa.properties.hibernate.jdbc.time_zone=UTC


        and it relies on using Instant for keeping the value on Java side and with created_at field having DATETIME type in MySQL and H2.



        The shortened resulting kotlin code is:



        @Entity
        data class SomeEntity(
        val createdAt: Instant = Instant.now() // default created date is current UTC time
        )

        val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

        createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter).toInstant(ZoneOffset.UTC)


        Ideas taken from comments of "Joop Eggen", this and this article.



        Bonus



        I guess if you're reading this, you might also need help with debugging SQL queries.



        1. To print SQL queries running on H2 add TRACE_LEVEL_FILE=2 and TRACE_LEVEL_SYSTEM_OUT=2 to connection string (see here):



        spring.datasource.url=jdbc:h2:mem:dbname;TRACE_LEVEL_FILE=2;TRACE_LEVEL_SYSTEM_OUT=2;


        2. To enable hibernate logs:



        spring.jpa.properties.hibernate.show_sql=true
        spring.jpa.properties.hibernate.use_sql_comments=true
        spring.jpa.properties.hibernate.format_sql=true
        logging.level.org.hibernate.type=TRACE


        3. To enable query logs in MySQL (one of the approaches, don't use on production db!):



        SET GLOBAL general_log = 'ON';
        SET global log_output = 'table';
        select * from mysql.general_log ORDER BY event_time DESC;





        share|improve this answer













        So looks like the fix was to set UTC timezone for the JDBC connection (instead of JVM):



        spring.jpa.properties.hibernate.jdbc.time_zone=UTC


        and it relies on using Instant for keeping the value on Java side and with created_at field having DATETIME type in MySQL and H2.



        The shortened resulting kotlin code is:



        @Entity
        data class SomeEntity(
        val createdAt: Instant = Instant.now() // default created date is current UTC time
        )

        val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")

        createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter).toInstant(ZoneOffset.UTC)


        Ideas taken from comments of "Joop Eggen", this and this article.



        Bonus



        I guess if you're reading this, you might also need help with debugging SQL queries.



        1. To print SQL queries running on H2 add TRACE_LEVEL_FILE=2 and TRACE_LEVEL_SYSTEM_OUT=2 to connection string (see here):



        spring.datasource.url=jdbc:h2:mem:dbname;TRACE_LEVEL_FILE=2;TRACE_LEVEL_SYSTEM_OUT=2;


        2. To enable hibernate logs:



        spring.jpa.properties.hibernate.show_sql=true
        spring.jpa.properties.hibernate.use_sql_comments=true
        spring.jpa.properties.hibernate.format_sql=true
        logging.level.org.hibernate.type=TRACE


        3. To enable query logs in MySQL (one of the approaches, don't use on production db!):



        SET GLOBAL general_log = 'ON';
        SET global log_output = 'table';
        select * from mysql.general_log ORDER BY event_time DESC;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 29 at 13:36









        WallTearerWallTearer

        1,3651 gold badge20 silver badges26 bronze badges




        1,3651 gold badge20 silver badges26 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55398279%2fmismatch-between-datetime-values-in-h2-and-mysql-databases-inserted-from-java-ko%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript