aspose file tools*
The moose likes Beginning Java and the fly likes Handling java.sql.Timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Handling java.sql.Timestamp" Watch "Handling java.sql.Timestamp" New topic
Author

Handling java.sql.Timestamp

An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
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

Joined: Jun 10, 2008
Posts: 156

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.



Priety.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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
Bartender

Joined: Oct 14, 2005
Posts: 18901
    
    8

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

Joined: Jan 17, 2007
Posts: 47
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

Joined: Jun 10, 2008
Posts: 156

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

Joined: Oct 13, 2005
Posts: 39828
    
  28
What is wrong with the methods of java.util.Date? Timestamp extends Date, if I remember correctly.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

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

Joined: Oct 13, 2005
Posts: 39828
    
  28
You're welcome. If the methods are deprecated, then be very careful to read the API before trying them.
Kiaamaa Liammes
Ranch Hand

Joined: Oct 03, 2009
Posts: 32

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


SCJP ,SCWCD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Handling java.sql.Timestamp