import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
public class Test{
public static void main(String[] args) throws ParseException{
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar gc=new GregorianCalendar(1000,1,29);
Date date=gc.getTime();
String sdate=dateFormat.format(date);
System.out.println(sdate);
}
}1000-02-29这一天真的存在么?
按照闰年规则,1000年应该不是闰年,但是sun API却输出了这一天!!!
GregorianCalendar 实现的是儒略历。格里高利历和儒略历之间的唯一区别就是闰年规则。儒略历指定每 4 年就为闰年,而格里高利历则忽略不能被 400 整除的世纪年。 文档原文:
The only difference between the Gregorian and the Julian calendar is the leap year rule. The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400.
在1582年之前的采用儒略历(Julian)--闰年以4整除为标准,之后的用格里高利历(Gregorian)--闰年为被4整除,并且百年的能被400整除。
判断闰年源码: public boolean isLeapYear(int year) {
if ((year & 3) != 0) {
return false;
} if (year > gregorianCutoverYear) {
return (year%100 != 0) || (year%400 == 0); // Gregorian
}
if (year < gregorianCutoverYearJulian) {
return true; // Julian
}
boolean gregorian;
// If the given year is the Gregorian cutover year, we need to
// determine which calendar system to be applied to February in the year.
if (gregorianCutoverYear == gregorianCutoverYearJulian) {
BaseCalendar.Date d = getCalendarDate(gregorianCutoverDate); // Gregorian
gregorian = d.getMonth() < BaseCalendar.MARCH;
} else {
gregorian = year == gregorianCutoverYear;
}
return gregorian ? (year%100 != 0) || (year%400 == 0) : true;
}
这10天不存在,虽然看完上面的说法,又翻了API文档已经大概了解了,不过还是非常新鲜,从来也没想到过有这样的事情!!!
return false;
}SUN还真是细节不错啊。