• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference beteween two dates  RSS feed

 
Gaurav Chhabras
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have to take out the difference betewen two dates like 1980/10/10 and 2000/05/05.

Could you please tell me that how will i will be able to calculate difference in Months.

Thanks
Regards
Gaurav
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.text.SimpleDateformat.parse method will convert those strings into Date objects. Then you can use Date.getTime to obtain a millisecond value of both dates. If you then subtract those from each other, and properly divide the result, you will get the number of months in between.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the answer for your example? 235? Or 234? Both could be acceptable depending on how you define the problem. So the first thing would be to specify what the question means.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Months are not a uniform size, so they are my cutoff in time units. By that I mean hours, minutes, seconds, days, weeks are all meaningful but months are confusing.

For example, what's January 31 + 1 month? What's January 31 + 1 month - 1 month?
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always translate my GregorianCalendar dates into milliseconds before I do any date calculations. Then you can just subtract to get the difference and do some math to calculate the months.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!