Solved

Java calendar calculate days between two months

Posted on 2010-11-17
2
597 Views
Last Modified: 2012-05-10
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
Comment
Question by:Greengiants15
2 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34157655
Correct me if i'm wrong but isn't this being done for the purposes of billing?
0
 
LVL 16

Accepted Solution

by:
imladris earned 500 total points
ID: 34158286
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 58
mapAB Challlenge 35 89
firstChar challenge 13 86
unix in java example 9 43
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now