ResultSet.getTimestamp(“date”) vs ResultSet.getTimestamp(“date”, Calendar.getInstance(tz))

后端 未结 1 992
余生分开走
余生分开走 2021-02-08 12:37

java.util.Date, java.util.Timetamp were seems to be causing great confusion for many. Within StackOverflow there are so many questions, Unfortunately m

1条回答
  •  [愿得一人]
    2021-02-08 12:50

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题