这是我更新的代码,它仍然不起作用。对于 2 月 29 日的所有情况,它返回天,当它应该只在闰年返回天时,如果不是闰年,应该返回 1。
public int checkDay (int day)
{
// For months with 30 days.
if ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 30)
return day;
// For months with 31 days.
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))
return day;
// For leap years.
// If February 29th...
if (month == 2 && day == 29)
{
// Check if year is a leap year.
if ((year%4 == 0 && year%100!=0) || year%400 == 0)
{
// If year is a leap year return day as 29th.
return day;
}
// If not a leap year, return day as 1st.
else return 1;
}
// If Date if February 1st through 28th return day, as it is valid.
if (month == 2 && (day >= 1 && day <= 28))
return day;
// Return day as 1st for all other cases.
return 1;
}