Reputation: 23443
From http://www.coderanch.com/t/381676/java/java/number-months-between-two-given, one post mentioned:
public static int monthsBetween(Date minuend, Date subtrahend)
{
Calendar cal = Calendar.getInstance();
// default will be Gregorian in US Locales
cal.setTime(minuend);
int minuendMonth = cal.get(Calendar.MONTH);
int minuendYear = cal.get(Calendar.YEAR);
cal.setTime(subtrahend);
int subtrahendMonth = cal.get(Calendar.MONTH);
int subtrahendYear = cal.get(Calendar.YEAR);
// the following will work okay for Gregorian but will not
// work correctly in a Calendar where the number of months
// in a year is not constant
return ((minuendYear - subtrahendYear) * cal.getMaximum(Calendar.MONTH)) +
(minuendMonth - subtrahendMonth);
}
Is it true that the number of months in a Calendar is not constant? And why?
Upvotes: 4
Views: 2041
Reputation: 45443
It is funny that, month comes from moon. A lunar calendar usually sync days with moon phases, so that, for example, day 15 of any month is always a full moon day.
The problem is a solar year is not exactly 12 moon cycles. So a lunar calendar must have "leap months".
Upvotes: -1
Reputation: 137382
Yes. In the hebrew calendar, there are several years with 13 months (7 out of 19 to be exact).
Upvotes: 9