When you assign a date to a named SQL parameter Hibernate automatically converts it to GMT time. How do you make it use the current server timezone for all dates?
Lets say you have a query:
Query q = session.createQuery("from Table where date_field < :now"); q.setDate("now", new java.util.Date());
"now" will be set to GMT time, while "new Date()" gets your current server time.
Thanks.
As it turned out Hibernate doesn't convert dates to GMT automatically, it just cuts off time if you use query. setDate() , so if you pass "2009-01-16 12:13:14" it becomes "2009-01-16 00:00:00". To take time into consideration you need to use query. setTimestamp("date", dateObj) instead.
As it turned out Hibernate doesn't convert dates to GMT automatically, it just cuts off time if you use query.setDate()
, so if you pass "2009-01-16 12:13:14" it becomes "2009-01-16 00:00:00".
To take time into consideration you need to use query.setTimestamp("date", dateObj)
instead.
Hibernate is ignorant of timezones. Any timezone conversion should be done prior to executing the query.
E.g., if your database server is set to CST, but the user is on EST, you'll need to add 1 hour to any timestamps which are the input to a query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With