Solved

Java calendar calculate days between two months

Posted on 2010-11-17
2
602 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

863 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

19 Experts available now in Live!

Get 1:1 Help Now