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
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.
posted 15 years ago
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
posted 15 years ago
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.