• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of days between dates

 
Varun Gokulnath
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like:
 
Alexey Dubinin
Greenhorn
Posts: 2
IntelliJ IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is bad solution because daylight saving changes can lead to error in calculations. You can try Joda time library.
 
Varun Gokulnath
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman but I don't think the "-" operator can be applied to two Date object operands.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that doesn't work well with daylight savings time.

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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Varun Gokulnath
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alexey, Seetharaman and Rob.
@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.

 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kevinj canlas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about in using a jcalendar




how to compute the number of days between two dates by using datechooser in java


dateee.png
[Thumbnail for dateee.png]
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were already provided an anwer here right? So what part are you having trouble with? Post your SSCCE code so we can help point you in the right direction
 
Winston Gutkowski
Bartender
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HIH

Winston
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varun

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?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston, James
Before you get too involved with Varun's problem, check the date of his original post.
 
Winston Gutkowski
Bartender
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....

Winston
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! Lets work out the number of days between the original post and now
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means not to use the no‑arguments Date() constructor. Look at its API to find out what they think you should use instead.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says to use GregorianCalendar, so I'm back at the first issue.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

And you're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic