• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling java.sql.Timestamp

 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have two date fields in database and I get them in my java as java.sql.Timestamp type variable. What is the best way to find the days difference between the 2 timestamp variables? (find no of days in between the 2 timestamp variables)?
Thanks,
An
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the public long getTime() of the Timestamp class on the Timestamp objects you receive.

Then compare the long values returned by this method.
Convert the time from milliseconds to seconds.
Take the difference and divide it by 3600 to get the difference between these to Timestamp objects in days.


 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priety Sharma wrote: . . . divide it by 3600 to get the difference between these to Timestamp objects in days.
By days, you mean hours, I presume?

There is bound to be another way to do it. What about the difference between 11.59pm Wednesday and 1am Friday? That will be 25 hours, 1 minute, which your method will show as 1 day. But the difference in dates is 2 days. I can't remember myself what to do, but somebody else is bound to know. Please wait.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who says the difference is 2 days? It's more like 1.0416667 days by my calculation.

But maybe An Sush doesn't like my calculation. That's no problem, it just means that An Sush has to define the correct calculation. But until that's done we can only post guesses here, and that isn't very helpful.
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Who says the difference is 2 days? It's more like 1.0416667 days by my calculation.

But maybe An Sush doesn't like my calculation. That's no problem, it just means that An Sush has to define the correct calculation. But until that's done we can only post guesses here, and that isn't very helpful.


I guess going with Priety's approach is fine..Dividing by 3600 will give me the exact days that have passed by after a time and not the calendar date...
Just out of curosity..suppose we had to go with the other way around where finding the calendar date difference is the prime target and actual time has no significance...how can this be implemented so that it works for all the scenarios (date, time, leap year, month all taken into consideration)
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi An,

Try this out for getting the dates instead of the time difference:

Call the getTime() method on the Timestamp objects -


Then create new GregorianCalendar objects using these long values:
Call the setTimeInMillis(long) on them:


Finally call the get() method which return the date and month and year as per your need:
Pass it the constants for day / month / year etc.


I hope this works for you and this is what you were looking for.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with the methods of java.util.Date? Timestamp extends Date, if I remember correctly.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Campbell.

Right everything I have put in that code can be done with just by using Date.

I did that cause most of the Date methods are deprecated.

Thanks for correcting me.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. If the methods are deprecated, then be very careful to read the API before trying them.
 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use java.util
Class Calendar , it has a lot of utility methods http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic