Solved

Java Calendar: Calculate Number of Months

Posted on 2010-11-17
12
780 Views
Last Modified: 2012-06-22
Hello Experts:

I'm trying to accurately calculate the number of months between two dates and I"m running into a tricky situation:

Let's say I have two dates:
start: 11/01/2010
end: 12/02/2010

I converted these dates strings into a Date object.  Now, I'm using the GregorianCalendar to calculate the number of months like so:

int months = ((end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) *  end.getMaximum(Calendar.MONTH) ) + end.get(Calendar.MONTH) - start.get(Calendar.MONTH);

This gives me the correct number of months, which is 1.

But if I change the dates like so:
start: 11/15/2010
end: 12/01/2010

I still get the number of months as 1, when really it should be 0.

Can anyone suggest a different and accurate way to calculate the number of months between two dates?  

Thank you!
0
Comment
Question by:Greengiants15
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34155837
Define 'month' please so that we help accurately
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34155862
try this,

int noOfMonth = TimeUnit.MONTH.convert( date1.getTime() - date2.getTime(), TimeUnit.MILLISECONDS ) ;
0
 

Author Comment

by:Greengiants15
ID: 34155887
Month would be the total number of months.

For example:
10/15/2010
to
12/15/2011

The number of months 14.

but
10/25/2010
to
12/15/2011
would be 13 months
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34155927
>>TimeUnit.MONTH

Which version of Java has the above?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34155939
>>Month would be the total number of months.

That's circular, so can't be used as the definition of month
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 100 total points
ID: 34155949
add a condition after you get the month:

if (day2<day1) {month = month - 1}
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Greengiants15
ID: 34156120
I'm running java 1.4.  TimeUnit is only good with 1.5
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34156152
>>TimeUnit is only good with 1.5

That's academic. afaics TimeUnit.MONTH doesn't exist
0
 
LVL 16

Accepted Solution

by:
HackneyCab earned 400 total points
ID: 34156401
I was bored, so I put this together. It satisfies all of your example dates and results, but you probably want to improve the error checking (such as better checking that the end date is actually later than the start date). Oh, and you'll need to change the exception handling, because my instant response to exceptions is to terminate the JVM.

public static int wholeMonthsPassed(String startDate, String endDate) {
	DateFormat df = new SimpleDateFormat("y/M/d");
	GregorianCalendar start = new GregorianCalendar();
	GregorianCalendar end = new GregorianCalendar();
	try {
		start.setTime(df.parse(startDate));
		end.setTime(df.parse(endDate));
	} catch (ParseException pe) {
		System.out.println("Failed to parse one or both date strings!");
		System.exit(1);
	}
	int startYear, startMonth, startDay;
	int endYear, endMonth, endDay;
	startYear = start.get(Calendar.YEAR);
	startMonth = start.get(Calendar.MONTH);
	startDay = start.get(Calendar.DAY_OF_MONTH);

	endYear = end.get(Calendar.YEAR);
	endMonth = end.get(Calendar.MONTH);
	endDay = end.get(Calendar.DAY_OF_MONTH);

	int yearsInMonths = 12 * (endYear - startYear);

	endMonth += yearsInMonths;

	int monthsPassed = endMonth - startMonth;

	if(endDay < startDay) {
		--monthsPassed;
	}

	if(monthsPassed < 0) {
		System.out.println("Cannot have end year earlier than start year!");
		System.exit(1);
	}

	return monthsPassed;
}

public static void main(String[] args) {
	String start = "2010/10/25";
	String end =   "2011/12/15";

	System.out.println("Result: "+wholeMonthsPassed(start, end));
}

Open in new window

0
 

Author Comment

by:Greengiants15
ID: 34156443
HainKurt: I like your suggestion.  That works but then it brings up another problem.

If the date is the following:
start: 10/5/2010
end: 11/1/2010

The number of months comes out as 0 (using your calculations) but if I try to calculate the number of days (start.get(Calendar.DAY_OF_MONTH) - end.get(Calendar.DAY_OF_MONTH))
I get 4 when I should get 27.
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 34156508
Oh, I should point out that my code is currently configured to parse dates in the most sensible format: year/month/date so if you must use the (very confusing) US format month/date/year, you'll need to change the call to SimpleDateFormat from ("y/M/d") to ("M/d/y").
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34156543
you get 4 (5-1 = 4)

if (start.get(Calendar.DAY_OF_MONTH) > end.get(Calendar.DAY_OF_MONTH)) {month = month - 1};

it does not calculate how many days between two dates, just checks the day number...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
SPLUNK REST  API call to Splunk to create and index? 2 67
Receive file in Servlet 1 36
count7 challenge 12 69
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

708 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