Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The number of months in a Calendar is not constant?

Tags:

java

calendar

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?

like image 657
Oh Chin Boon Avatar asked Feb 24 '23 09:02

Oh Chin Boon


1 Answers

Yes. In the hebrew calendar, there are several years with 13 months (7 out of 19 to be exact).

like image 110
MByD Avatar answered Mar 07 '23 11:03

MByD