• Post Reply Bookmark Topic Watch Topic
  • New Topic

accurate time difference betwwen two time stamp  RSS feed

 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how i can i calculate time difference between two time stamp like 21-08-2013 10:00:00 am and 22-08-2013 2:00:00 pm. i want accurate difference of days,hour, minute and second in java.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code
1) Use SimpleDateFormat to obtain the Date objects
2) Obtain the long long value from the date object
3) 1 second = 1000 ms. 1 minute = 60 seconds. 1 hour = 60 minutes. 1 day = 24 hours.
4) Use #3 to compute the difference.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could replace steps 3 and 4 by something from the Joda Time library, which makes calculations such as this one fairly easy and readable.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:Or you could replace steps 3 and 4 by something from the Joda Time library, which makes calculations such as this one fairly easy and readable.

@viral: While I totally agree with Jan's suggestion, the fact is that (at least as far as I've heard) version 8 of Java is going to include a new Date/Time API, based on - but not identical to - Joda Time.

My suspicion is that they will have a lot in common, so learning Joda Time will probably give you a good head start, but if you're going to be using dates and times heavily, you might want to wait until version 8 is out (March 2014); or at least familiarise yourself with the likely changes (see the JSR-310 page), so that you can design your API to change to version 8 with the minimum of fuss.

HIH

Winston
 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you provide me code in details with example. Thanks in Advance.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
viral thakar wrote:can you provide me code in details with example. Thanks in Advance.

Sorry. The Ranch is NotACodeMill
Tell us what you have tried, where you are stuck, post your SSCCE and we will help point you in the right direction.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!