Java ResultSet.getTimestamp("date") vs ResultSet.getTimestamp("date", Calendar.getInstance(tz))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2584695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:30:27  来源:igfitidea点击:

ResultSet.getTimestamp("date") vs ResultSet.getTimestamp("date", Calendar.getInstance(tz))

javaoraclejdbcsybase

提问by Mohan Narayanaswamy

java.util.Date, java.util.Timetampwere seems to be causing great confusion for many. Within StackOverflow there are so many questions, Unfortunately my question is bit twisted.

java.util.Datejava.util.Timetamp似乎给许多人造成了很大的困惑。在 StackOverflow 中有很多问题,不幸的是我的问题有点扭曲。

There are 2 JDBC api. How they should perform? Was there any consistencies among RDBMS'es?

有2个JDBC API。他们应该如何表现?RDBMS 之间是否存在任何一致性?

ResultSet.getTimestamp("dateColumn") 
ResultSet.getTimestamp("dateColumn", Calendar.getInstance(tz))

If someone has knowledge in Sybase, could you please share your experience?

如果有人了解Sybase,能否分享一下您的经验?

采纳答案by BalusC

First, you're confusing java.utilwith java.sql. When using PreparedStatement#setDate()and ResultSet#getDate(), you need java.sql.Date. Analogous, when using PreparedStatement#setTimestamp()and ResultSet#getTimestamp()you need java.sql.Timestamp.

首先,你混淆java.utiljava.sql. 使用PreparedStatement#setDate()and 时ResultSet#getDate(),您需要java.sql.Date. 类似的,当使用PreparedStatement#setTimestamp()并且ResultSet#getTimestamp()你需要java.sql.Timestamp.

Second, it's important to understand that java.sql.Daterepresents solely the date(year, month, day) and nothing less or more. This is to be mapped to a SQL DATEfield type. The java.sql.Timestamprepresents the timestamp(year, month, day, hour, minute, second, millisecond), exactly as the java.util.Dateand java.util.Calendardoes. This is to be mapped to a SQL TIMESTAMPor DATETIMEfield type.

其次,重要的是要理解它java.sql.Date只代表日期(年、月、日),不能少也不能多。这将被映射到 SQLDATE字段类型。该java.sql.Timestamp代表时间戳(年,月,日,时,分,秒,毫秒),完全一样的java.util.Datejava.util.Calendar做。这将被映射到 SQLTIMESTAMPDATETIME字段类型。

As to the timezones, you need it when the database does not store timezone information (thus, all timestamps are stored in UTC (GMT)). You can then pass a Calendarin which contains information about the current timezone, so that the JDBC driver can adjust the UTC timestamp to the timestamp conforming the timezone. If it is for example GMT+1, then the JDBC driver will add one hour to the timestamp before returning.

至于时区,当数据库不存储时区信息时需要它(因此,所有时间戳都以UTC(GMT)存储)。然后,您可以传入Calendar包含有关当前时区信息的in,以便 JDBC 驱动程序可以将 UTC 时间戳调整为符合时区的时间戳。例如,如果是 GMT+1,则 JDBC 驱动程序将在返回之前将时间戳添加一小时。