I'm trying to write a program that computes the number of days between two dates. Can anyone suggest how i can achieve this. I'm quite sure I'm supposed to use the Calendar class. Thanks in advance.
In pseudo code, assuming first <= second:
A bit more advanced:
This latter can be explained with two examples:
1) February 1st 2010 and March 1st 2011.
count = (31 + 28 + 1) - (31 + 1) = 28.
2010 < 2011 so count += 365 => count = 393.
2) March 1st 2010 and February 1st 2011.
count = (31 + 1) - (31 + 28 + 1) = -28 (yes, negative; that'll be corrected).
2010 < 2011 so count += 365 => count = 337.
The methods to use from Calendar are add for adding a day / year, and getActualMaximum to get the numbers in the year. getActualMaximum will return 365 or 366, getMaximum will always return 366.
Alexey Dubinin wrote:This is bad solution because daylight saving changes can lead to error in calculations.
thanks for remembering the daylight saving problem .
Varun Gokulnath wrote:Thanks Seetharaman but I don't think the "-" operator can be applied to two Date object operands.
getTime returns long
@Seetharaman : I am able to get a good estimate by using your method
@Rob:Can you tell me if I am using your method correctly because I am not arriving at the right answer.
Varun Gokulnath wrote:
That doesn't compare the days; that compares the time in milliseconds after casting it to int. No idea why you did that cast anyway. Without it you should be very close, except if c1's hour would be one larger than c2's hour then you would get one day less.
Anyway, to compare if two dates are the same date you need to check two or three fields:
1) are the year, the month and the day of month the same.
2) are the year and day of year the same.
Varun Gokulnath wrote:I'm trying to write a program that computes the number of days between two dates. Can anyone suggest how i can achieve this. I'm quite sure I'm supposed to use the Calendar class. Thanks in advance.
Are you "quite sure [you're] supposed to use the Calendar class" because you think you need to, or because you were told to?
There's a big difference, because you DON'T need a Calendar to compute the number of days between two dates - at least not if they're java.util.Date's - and actually it probably muddies the waters.
Furthermore, you don't even need to "estimate" it, viz from your own code:The method returns an extremely accurate computation of the number of days difference, which you can then truncate or round or convert to a smaller unit any way you see fit.
NOTE: The second thing that has been discussed here (seeing whether two dates are on the same day) is something completely different, because that is a comparison of WALL-CLOCK times - and for that, you DO need a Calendar ... and possibly an explicit timezone.
What stage are you at with your code? Could you show us what you have done so far? Winston makes a good point regarding Calendar. Is the starting point for your work dates which are entered by a user / read from some data source or does the code simply have to calculate the difference in days between 2 random dates?
Before you get too involved with Varun's problem, check the date of his original post.
Stuart A. Burkett wrote:Before you get too involved with Varun's problem, check the date of his original post.
Dang! Always catches me out. Don't I feel the idiot....
I've tried several things and just don't understand the calendar class.
The first way I did was this:
This returns an error in the long diff1 where I can't subtract a date from a date.
The other thing I tried was this:
This gives me the errors below but if I ignore those and run it the answer comes out to be 59967771772956 days, which is clearly off by a day or two I haven't even tried trying to calculate for next summer. Am I missing something basic, like somehow converting the date to an integer?
Note: Summer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
You can get Date objects from the Calendar objects; again look at the old posts. That gets you out of having to use the deprecated constructor.
if it's the best way but at least I don't want to throw my computer out the window right this second
And you're welcome