• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused about SQL Timestamp

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic