This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
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?
I'm working on trying to calculate the difference between today and the first day of summer (June 21) for this year and the difference for next year.
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.
If you use getTime, you get the duration since 1st January 1970 in milliseconds (I think), as a long. You can see that sort of thing from the older posts.
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.
Joined: Feb 26, 2014
I can't thank you enough for all the help you guys have given me. I redid it like below and it works. I'm not sure
if it's the best way but at least I don't want to throw my computer out the window right this second
Joined: Oct 13, 2005
Don't use 5 or 11; use Calendar.JUNE, Calendar.DECEMBER, etc. Remember that technique rounds down the number of days; from 11.59pm Saturday to 11.58pm and 59″ Sunday counts as 0 days.