• Post Reply Bookmark Topic Watch Topic
  • New Topic

exact date difference using Gregorian Calendar  RSS feed

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am trying to find out the exact difference between two dates either in no.of.years or no.of.months. I am using the GregorianCalendar api for the same.
Here is the excerpt of the code
-----------------
while (gc1.before(gc2)) {
gc1.add(type,1); // type is Calendar.YEAR
elapsed++;
}
------------------
This always give the difference+1 as even when gc1 (yr or month) is the same as gc2 (yr or month) the while block executes. How can this be resolved using the GregorianCalendar api.

Do post your thoughts

Uday

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subtract 1 from the result.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If gc1.before(gc2) returns true even though month and year are equal, then that must mean that day of month and/or time are not equal. So you should check your code, particularly the parts related to setting gc1 and gc2. If you do not want to use time in your comparison, you should make sure it remains 0.
 
Udayan Kumar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arjan,

I am using gregorianCalendar.clear methods (if the request is month i clear the date) and when it is clear we clear the date and month.
Is this what you mentioned in your post.

Uday
 
Arjan Kleene
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tested the date comparison, I simply used the date in the constructor. Something like gc1 = new GregorianCalendar(2009, 1, 31); This sets hour, minute, second and millisecond to the default value (0). It doesn't take into account that you might have different timezones, but if you just want to know the number of days/months/years then timezone doesn't really matter. If you don't specify a timezone then the default timezone will be used. So you still get the same timezone for all dates.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With years and months it's quite easy to calculate the difference. For years it is simply
For months it is similar:
This code does assume that each year has the same number of months though. If not you will have to loop:
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot for a moment that months are 0 based, so should be changed into
The same code without the +1 can also be used for Calendar.DAY_OF_YEAR and Calendar.WEEK_OF_YEAR.

And finally the generic code for months, weeks and days:

The minimum will be 0 for months, 1 for weeks and days. The + 1 is required because both the maximum and minimum are inclusive. I've tested it and compared it to VisualBasic's DateDiff method, with equal results for all three fields.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!