aspose file tools*
The moose likes Beginning Java and the fly likes Number of days between dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Number of days between dates" Watch "Number of days between dates" New topic
Author

Number of days between dates

Varun Gokulnath
Greenhorn

Joined: May 22, 2011
Posts: 7
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

Joined: Jan 28, 2008
Posts: 5575

something like:
Alexey Dubinin
Greenhorn

Joined: Mar 23, 2010
Posts: 2

This is bad solution because daylight saving changes can lead to error in calculations. You can try Joda time library.
Varun Gokulnath
Greenhorn

Joined: May 22, 2011
Posts: 7
Thanks Seetharaman but I don't think the "-" operator can be applied to two Date object operands.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 22, 2011
Posts: 7
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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

Joined: Feb 06, 2013
Posts: 5
how about in using a jcalendar




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




[Thumbnail for dateee.png]

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8249
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: May 30, 2012
Posts: 679
Winston, James
Before you get too involved with Varun's problem, check the date of his original post.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8249
    
  23

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

Joined: Nov 09, 2011
Posts: 1030
    
    5

Excellent! Lets work out the number of days between the original post and now
Ryan Bishop
Ranch Hand

Joined: Feb 26, 2014
Posts: 138
    
    5
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

Joined: Oct 13, 2005
Posts: 39882
    
  28
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

Joined: Feb 26, 2014
Posts: 138
    
    5
It says to use GregorianCalendar, so I'm back at the first issue.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
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

Joined: Feb 26, 2014
Posts: 138
    
    5
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

Joined: Oct 13, 2005
Posts: 39882
    
  28
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of days between dates