This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about SQL Timestamp

 
Mike London
Ranch Hand
Posts: 1185
4
  • Mark post as helpful
  • send pies
  • 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
 
Ken Robinson
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1185
4
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic