来自http://www.coderanch.com/t/381676/java/java/number-months-between-two-given,提到的一篇文章:
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);
}
Run Code Online (Sandbox Code Playgroud)
日历中的月数不是恒定的吗?为什么?
| 归档时间: |
|
| 查看次数: |
1342 次 |
| 最近记录: |