• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Handling java.sql.Timestamp

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 27228
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. If the methods are deprecated, then be very careful to read the API before trying them.
 
Ranch Hand
Posts: 32
Eclipse IDE Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic