supertonsky
supertonsky

Reputation: 2743

How do I set JDBC's timezone on a query basis?

One way to do this is the following.

TimeZone.setDefault(TimeZone.getTimeZone("yourTimeZone"));

However, it will affect the entire JVM. Is there a way to do it on a query basis that is safe and will not have impact to the rest of the application or JVM that may have assumed a specific timezone?

Upvotes: 0

Views: 213

Answers (1)

Andreas
Andreas

Reputation: 159185

Specify the time zone you want when setting/getting Timestamp values.

PreparedStatement.setTimestamp(int parameterIndex, Timestamp x, Calendar cal)

With a Calendar object, the driver can calculate the timestamp taking into account a custom timezone. If no Calendar object is specified, the driver uses the default timezone, which is that of the virtual machine running the application.

ResultSet.getTimestamp(String columnLabel, Calendar cal)

This method uses the given calendar to construct an appropriate millisecond value for the timestamp if the underlying database does not store timezone information.

Both use the time zone of the calendar:

Calendar.getInstance(TimeZone zone)

Gets a calendar using the specified time zone and default locale.

Calendar.setTimeZone(TimeZone value)

Sets the time zone with the given time zone value.

Upvotes: 2

Related Questions