ResultSet.getTimeStamp()

R

RAMK

Hi,
I have seen in the java doc of ResultSet.getTimeStamp(String,
Calendar). But I could not understand the significance of Calendar
object. Can you please tell me whats the purpose of Calendar object
here...
In addition to this, shortly this is my problem: I have a date field in
the database(oracle) like 01-01-1970 10:00. There is no timezone
information associated with this as it is a normal date type in the
database. Now my JVM is in different timezone. What I require is- this
date(in the database) to be read as it is into my java code using
getTimeStamp() without any timezone conversions. My assumption is that
there exists some default timezone conversion by JDBC driver while
reading the date from the database.
Please clarify...

Much appreciated,
Ramesh
 
P

P.Hill

RAMK said:
Hi,
I have seen in the java doc of ResultSet.getTimeStamp(String,
Calendar). But I could not understand the significance of Calendar
object. Can you please tell me whats the purpose of Calendar object
here...

Well, it's just what you need!

If we read the JavaDoc, it says:
"This method uses the given calendar to construct an appropriate
millisecond value for the timestamp if the underlying database does not
store timezone information."

-- http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html

and you said:
I have a date field in
the database(oracle) like 01-01-1970 10:00. There is no timezone
information associated with this as it is a normal date type in the
database.

We need to go from a String "01-01-1970 10:00"
to a millisecond value (10 hours worth of milliseconds since 1/1/1970)
because that is what a java.util.Date uses to represent a datetime.
But 10 in London is not 10 in Tokyo nor the same milliseconds as
10 in San Francisco.

The Calendar provides the missing information. It is the algorithm
for converting from a set of fields to a millisecond value. The
most important part of the algorithm in this case is the timezone.
Now my JVM is in different timezone. What I require is- this
date(in the database) to be read as it is into my java code using
getTimeStamp() without any timezone conversions.

You can set the TZ in the calendar passed to whatever you want,
that is why there is an explicit calendar on this call.
Maybe you'd want the local TZ, or maybe GMT (so you get 10 hours worth
of milliseconds). As you stated you want the second case, so
create a Calendar and set its TZ with something like
cal.setTimeZone( TimeZone.getTimeZone( "GMT" ));
My assumption is that
there exists some default timezone conversion by JDBC driver while
reading the date from the database.

Yes, the default used is the default for the VM as set when the VM
started or overridden via TimeZone.setDefault(), but don't bother with
"moving" the VM, just tell the ResultSet you want something different.

HTH,
-Paul
 
L

Lee Fesperman

RAMK said:
.... My assumption is that
there exists some default timezone conversion by JDBC driver while
reading the date from the database.
Please clarify...

Yes, but that is entirely up to the JDBC driver. SQL92 supports Time/Timestamp both with
and without a timezone specification. You could assume that the ones without a tz
specification would use the timezone of the server on the server and would convert to
the client's tz when transferring without conversion. For instance, converting the value
to string on the server would get the server one. The getTimestamp() method with a
Calender object is intended to force a specific tz rather than the current default.
However, there are no guarantees in any case; check the docs for your driver/backend or
test.

BTW, the support for SQL92 Time/Timestamp with Timezone seems to be rare.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top