简体   繁体   English

任何人都可以解释jdbc结果集getTimestamp(int,calendar)的用法吗?

[英]Can anyone explain the usecase of jdbc resultset getTimestamp(int,calendar)?

I'm talking about this interface method: 我在谈论这个接口方法:

http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getTimestamp%28int,%20java.util.Calendar%29 http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getTimestamp%28int,%20java.util.Calendar%29

The most commonly used implementation being the one in cachedrowset: 最常用的实现是cachedrowset中的实现:

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/com/sun/rowset/CachedRowSetImpl.java line 6170 http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/com/sun/rowset/CachedRowSetImpl.java line 6170

You will notice that the implementation does two very weird things: 您会注意到实现有两个非常奇怪的事情:

1) it modifies the calendar passed as argument, even though there is also a return value 1)它修改作为参数传递的日历,即使还有返回值

2) it extracts all the time information from SQL, except for the milliseconds , which come from the calendar passed as argument. 2)它从SQL中提取所有时间信息, 除了毫秒 ,它来自作为参数传递的日历。

The interface description is rather unclear, but assuming the implementation is correct - What is the point of this method? 界面描述相当不清楚,但假设实现是正确的 - 这个方法有什么意义? I can understand a method that would take a calendar to extract just the timezone, without modifying it. 我可以理解一种方法,它可以使日历只提取时区,而无需修改它。 But taking a calendar, modifying it, and extracting not only the zone but also the milliseconds... 但是拿一个日历,修改它,不仅提取区域而且提取毫秒......

Does anyone have any insight as to the history/design/reasoning behind this API? 有没有人对这个API背后的历史/设计/推理有任何见解?

This seems to be an incorrect interpretation of the JDBC API documentation (and unfortunately the javax.sql.rowset has more of those incorrect interpretations). 这似乎是对JDBC API文档的错误解释(不幸的是, javax.sql.rowset有更多这些错误的解释)。

By default a JDBC driver needs to store or retrieve a time or timestamp as if the time stored in the database is in the current timezone of the JVM. 默认情况下,JDBC驱动程序需要存储或检索时间或时间戳,就好像存储在数据库中的时间位于JVM的当前时区中一样。 As this isn't always what you want, the API provides methods to provide a Calendar object, which you need to use to derive the actual timezone to use (I don't know why they didn't use java.util.TimeZone instead). 由于这并不总是你想要的,API提供了提供Calendar对象的方法,你需要使用它来派生实际时区(我不知道为什么他们不使用java.util.TimeZone而是)。

This is specified in PreparedStatement.setTimeStamp : 这在PreparedStatement.setTimeStamp中指定:

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对象,则驱动程序将使用默认时区,即运行应用程序的虚拟机的时区。

The method getTimestamp in ResultSet follows (or should follow) these same rules. ResultSet的方法getTimestamp遵循(或应该遵循)这些相同的规则。

So if a database stores time '11:20' and your local timezone is CET (UTC+1), then the retrieved time is 10:20 UTC (11:20 CET). 因此,如果数据库存储时间'11:20'并且您的本地时区是CET(UTC + 1),则检索的时间是10:20 UTC(欧洲中部时间11:20)。 However if I provide a Calendar in GMT, the returned time should be 11:20 UTC (12:20 CET). 但是,如果我在GMT中提供Calendar ,则返回的时间应为UTC时间11:20(欧洲中部时间12:20)。

SQL databases store the date and time value for timestamps in the form of year, month, day, hour min, sec etc. The instance in time these values specify depends on the timezone in which they are interpreted (eg 2014. january 1. 15:00:00 is not at the same time in Europe than in the USA). SQL数据库以年,月,日,小时,秒等形式存储时间戳的日期和时间值。这些值指定的时间实例取决于它们被解释的时区 (例如2014年1月1日.15 :00:00在欧洲不是在美国的同一时间。 The timezone may or may not be part of the timestamp, depending on the column's type. 时区可能是可能不是时间戳的一部分,具体取决于列的类型。

The Java java.sql.Timestamp and java.util.Date classes represent an instance of time regardless of the time zone (or rather in a fixed, UTC timezone). Java java.sql.Timestampjava.util.Date类表示时间的实例,与时区无关(或者更确切地说,在固定的UTC时区中)。

If the column in the SQL database does not store the timezone info along with the date+time, in order to create a Java Date or TimeStamp object from such a timestamp requires a timezone so it can point to a specific instance in time (in the reference UTC timezone). 如果SQL数据库中的列没有存储时区信息以及日期+时间,那么为了从这样的时间戳创建Java DateTimeStamp对象需要一个时区,以便它可以及时指向特定的实例(在参考UTC时区)。

The ResultSet.getTimestamp() method in question can be used to get an SQL timestamp data and convert it to a Java Timestamp instance using the timezone info set in the parameter Calendar object. 有问题的ResultSet.getTimestamp()方法可用于获取SQL时间戳数据,并使用参数Calendar对象中设置的时区信息将其转换为Java Timestamp实例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM