Hibernate query to get server time without converting to local timezone

Viewed 642

I've searched high and low for an answer to this and I've found nothing. Note (before someone refers me to a similar, but unhelpful question) that I'm NOT STORING ANY DATES/TIMES IN THE DATABASE.

Background

I have a Postgres database that's running on a server somewhere. I have a Java back end that queries the database using Hibernate. The database server and the Java back end (Tomcat server) are running on separate systems, and can be in different timezones.

The server needs to perform some cleanup tasks at a specific time (which can be configured by the user) - for example, delete entries older than x days every day at noon. I'm trying to run a query on the server to get the current server time. The problem is that either hibernate or the JVM is converting the time from the time on the database server to the local time on the server running Tomcat.

Example

I have a database server (Postgres) on a server using Central Time (UTC-6). I have my back end server (Java/Tomcat) using GMT (UTC+0). I have a query that I can run to get the current time (SELECT now()). Let's say that the current time on the database server is 12:00:00. If I run the query from the back end server on the database server using pgAdmin, it returns the correct time (12:00:00). If I run the same query using Hibernate, it converts it to GMT (18:00:00).

Question

Is there a query that I can run from the back end server (Java/Tomcat) that will get the current time from the database server without converting it? Alternatively, is there something that I can do to the time that's returned so that I can convert it back to the time on the database server?

1 Answers

Time on Tomcat server is 18:00 then below sql would return 12:00...

select current_timestamp - (interval '6' hour)
Related