Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

Java calendar calculate days between two months

Hi Experts:
I have one final problem that I need help with.  I need to calculate the number of days between two months.
I have two dates:
start: 11/16/2010
end: 12/01/2010

I need to calculate the number of days between these two months.  In this case, the answer would be: 16

[i]BUT [/i]It can get tricky if the dates are like this:
start: 11/15/2010
end: 12/21/2010

In this case the number of days is 6.  The reason why it should be 6 is because it's 1 month + 6 days.  
Tricky, I know!
0
Greengiants15
Asked:
Greengiants15
1 Solution
 
CEHJCommented:
Correct me if i'm wrong but isn't this being done for the purposes of billing?
0
 
imladrisCommented:
How about something like this:

If the date of the end date is greater than or equal to the date of the beginning date, then simply subtract the two dates (21-15=6).

If not, then add the date of the end date to the length of the month prior to the end date, and then subtract the dates ((1+30)-16=15).

Note that there is discrepancy in how your counting days in the two cases. If that is intentional you will have to compensate by adding a day in the second case.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now