开发者

The number of months in a Calendar is not constant?

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 开发者_StackOverflow中文版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?


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


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜