Reputation: 4202
Is there a way to get database timezone using HQL? Will the same HQL work for all databases?
Upvotes: 0
Views: 717
Reputation: 5293
The timezone conversions are done by the underlying JDBC driver (and not by Hibernate). In this way the server timezone is hidden from the application. As far as I know there is no "official" way to read the server timezone.
On Oracle databases you can read the server timezone with
SELECT DBTIMEZONE FROM dual;
For MySQL you do
SELECT @@global.time_zone, @@session.time_zone;
But as you can see this does not work for all databases.
You can configure the database timezone in the JDBC connect string (in your hibernate.cfg.xml), but that is even more database dependent.
An other solution is to define a system property, which has to specified when starting your application with -Dserver-timezone=...
and in your application you can read it by
String tz = System.getProperty("server-timezone");
In this way it works for all databases, but you have to specify it in your startup script.
Upvotes: 2