简体   繁体   English

如何基于查询设置JDBC的时区?

[英]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. 但是,它将影响整个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? 有没有一种方法可以在安全的查询基础上进行查询,并且不会对可能具有特定时区的其余应用程序或JVM产生影响?

Specify the time zone you want when setting/getting Timestamp values. 指定设置/获取Timestamp值时所需的时区。

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

With a Calendar object, the driver can calculate the timestamp taking into account a custom timezone. 使用Calendar对象,驱动程序可以考虑自定义时区来计算时间戳。 If no Calendar object is specified, the driver uses the default timezone, which is that of the virtual machine running the application. 如果未指定Calendar对象,则驱动程序将使用默认时区,即运行应用程序的虚拟机的默认时区。

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. 使用给定的时区值设置时区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Selenium Chromedriver 中设置时区? - How do I set a timezone in Selenium Chromedriver? 如何为UniObjects设置时区? - How do I set the timezone for UniObjects? 如何使用TimeZone设置日历? - How do I set a Calendar with TimeZone? 如何在每个请求的基础上设置杰克逊的时区? - How to set timezone of jackson on a per-request-basis? 在JDBC中,当调用PreparedStatement.setArray()时,如何为时间戳数组指定时区? - In JDBC, when calling PreparedStatement.setArray(), how do I specify the timezone for an array of timestamps? 如何在 Tomcat 中为单个 Web 应用程序设置时区? - How do I set the timezone in Tomcat for a single web app? 如何在单个类的基础上在JAXB中设置Element的elementFormDefault而不是为整个包设置? - How do I set the elementFormDefault of an Element in JAXB on a single class basis instead of having it set for the entire package? 如何使用jdbc将数据插入到MySQL数据库的SET列中? - How do I insert data into a SET column for a mysql database with jdbc? 如何根据条件设置Textview? - How can I set the Textview on the basis of a condition? 如何设置JDBC连接的时区区域并避免找不到SqlException时区区域? - How to set the timezone region for JDBC Connection and avoid the SqlException timezone region not found?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM