Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Java calendar calculate days between two months

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

860 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