How can I explicitly set the time zone H2 should use? Now it gets the timezone to use from the underlying OS. I would assume there existed an extra parameter I would add to
I suppose you see such "misbehaviour" when reading types Date
or DateTime
.
Apparently H2 uses different time zone information during write and read. Providing a calendar with expected timezone returns the expected values for me:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(ZoneOffset.UTC))
resultSet.getDate("dateColumn", cal)
H2 uses JVM timezone and it affects your Date calculation. If you are using it in Junits for example, you can set a certain timezone then re-put initial value when done. Example:
System.setProperty("user.timezone", "GMT-3");
TimeZone.setDefault(null);
Apparently you don't have a parameter on the connection but the database will use the timezone of the JVM where the driver is loaded so you can set -Duser.timezone=UTC
.
Note that you can't change the timezone after the driver has been loaded.
You can't set the timezone for the database.
But, I'm not aware that H2 uses a timezone (at least not a current version of H2) for most operations.
So, what problem do you want to solve?
What helped me was to set timezone config for JDBC instead of JVM, which also seems more reasonable and cleaner way, as it affects only the database instead of the whole JVM:
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
My answer to the other question might help with additional info.
You can manipulate the JVM time zone, before interacting with the database:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
Unfortunately, H2 does not support time zone per connection... yet.