提交 e080ea15 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Extract getDaysInMonth() from DateTimeUtils.isValidDate()

上级 8678a122
......@@ -877,6 +877,20 @@ public class DateTimeUtils {
}
}
/**
* Returns number of days in month.
*
* @param year the year
* @param month the month
* @return number of days in the specified month
*/
public static int getDaysInMonth(int year, int month) {
if (month != 2) {
return NORMAL_DAYS_PER_MONTH[month];
}
return (year & 3) == 0 && (year < 1582 || year % 100 != 0 || year % 400 == 0) ? 29 : 28;
}
/**
* Verify if the specified date is valid.
*
......@@ -889,24 +903,11 @@ public class DateTimeUtils {
if (month < 1 || month > 12 || day < 1) {
return false;
}
if (year > 1582) {
// Gregorian calendar
if (month != 2) {
return day <= NORMAL_DAYS_PER_MONTH[month];
}
// February
if ((year & 3) != 0) {
return day <= 28;
}
return day <= ((year % 100 != 0) || (year % 400 == 0) ? 29 : 28);
} else if (year == 1582 && month == 10) {
if (year == 1582 && month == 10) {
// special case: days 1582-10-05 .. 1582-10-14 don't exist
return day <= 31 && (day < 5 || day > 14);
}
if (month != 2 && day <= NORMAL_DAYS_PER_MONTH[month]) {
return true;
return day < 5 || (day > 14 && day <= 31);
}
return day <= ((year & 3) != 0 ? 28 : 29);
return day <= getDaysInMonth(year, month);
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论