Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

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

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

like image 597
Mohan Narayanaswamy Avatar asked Apr 06 '10 12:04

Mohan Narayanaswamy


People also ask

How do you find the ResultSet of a date?

Execute the query using the executeQuery() method. Pass the select query to retrieve data (String) as a parameter to it. From the obtained result set object get the date value (along with other values) using the getDate() method of the ResultSet interface. Pass the name of the column (String) as a parameter to this.

Does SQL timestamp have timezone?

The timestamp datatype allows you to store both date and time. However, it does not have any time zone data. It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically.

Does Java timestamp have timezone?

Find Current Date Timestamps in Java Timezone ExampleGMT timezone is sort of standard timezone for recording date and timestamp. if your application is not using any local timezone that you can use GMT for storing values on server.

What is ResultSet next in Java?

The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only.


1 Answers

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.

like image 85
BalusC Avatar answered Sep 25 '22 04:09

BalusC