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
ResultSet.getTimestamp("date") vs ResultSet.getTimestamp("date", Calendar.getInstance(tz))
提问by Mohan Narayanaswamy
java.util.Date
, java.util.Timetamp
were seems to be causing great confusion for many. Within StackOverflow there are so many questions, Unfortunately my question is bit twisted.
java.util.Date
,java.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.util
with 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.util
了java.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.Date
represents solely the date(year, month, day) and nothing less or more. This is to be mapped to a SQL DATE
field type. The java.sql.Timestamp
represents the timestamp(year, month, day, hour, minute, second, millisecond), exactly as the java.util.Date
and java.util.Calendar
does. This is to be mapped to a SQL TIMESTAMP
or DATETIME
field type.
其次,重要的是要理解它java.sql.Date
只代表日期(年、月、日),不能少也不能多。这将被映射到 SQLDATE
字段类型。该java.sql.Timestamp
代表时间戳(年,月,日,时,分,秒,毫秒),完全一样的java.util.Date
和java.util.Calendar
做。这将被映射到 SQLTIMESTAMP
或DATETIME
字段类型。
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 Calendar
in 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 驱动程序将在返回之前将时间戳添加一小时。