This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Confused about SQL Timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Confused about SQL Timestamp" Watch "Confused about SQL Timestamp" New topic
Author

Confused about SQL Timestamp

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
If you have code where you read a SQL timestamp from a database and then want to break it apart, I tried to use code like this, which works:
Calendar cal = Calendar.getInstance();
long millis = cal.getTime().getTime();
Timestamp ts = new Timestamp(millis);
System.out.println("Day: " + ts.getDate()) // using timestamp!

However, the API says getDate() is deprecated and use a format like this:
Calendar.get(Calendar.MINUTE)
However, given that you're staring with a SQL Timestamp value, I can't see how to use this "correct" form of the Calendar.
This is way more difficult than it should be.
Can anybody lend me a hand?
All I'm really trying to do is to see if one SQL Timestamp is more than, say, 10 minutes older than another.
Thanks very much in advance.
-- Mike
Ken Robinson
Ranch Hand

Joined: Dec 23, 2003
Posts: 101

Calendar is an abstract class with GregorianCalendar the only subclass in JDK 1.3. There are several others available on the web for calendars such as the Arabic, Chinease and Jewish Calendars. A Date (java.util or java.sql or java.sql.TimeStamp) is nothing more than the number of milliseconds after a set date. The Date methods where deprecated to force the use of a specific Calendar that is specific to the type of real life Calendar required. The Calendar object will 'translate' the raw date to the Calendar specific Date/Time you require.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
I don't believe that code would work with a *SQL* Timestamp. <s>
However, I just noticed that in some later JDK (later than my printed copy of the JDBC reference, anyway), they added a getTime() and setTime() method to the Timestamp class.
So, now you can extract the milli-seconds from two Timestamp objects and compare them any way you want.
Thanks for your reply.
-- Mike
Ken Robinson
Ranch Hand

Joined: Dec 23, 2003
Posts: 101
java.sql.TimeStamp extends java.util.Date. Any object/method, including Calendar, that requires a java.util.Date will accept a java.sql.TimeStamp.
java.sql.Date, java.util.Time and java.sql.TimeStamp all extend java.util.Date for the basic purpose of overriding the toString() method, which outputs the date in either a SQL Date or Timestamp String.
If you have a java.util.Date object, you can assign it the return value of ResultSet.getDate, ResultSet.getTime and ResultSet.getTimeStamp.
 
 
subject: Confused about SQL Timestamp
 
Similar Threads
Dealing with results of java time code
java.sql.Date - setting a future date?
primary key generation and concurrency
Wrong Hour/Timezone
TimeStamp.toString() does not work as expected