gpt4 book ai didi

Java 有效日期闰年

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:48:21 25 4
gpt4 key购买 nike

这是我更新后的代码,还是不行。对于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;
}

最佳答案

试试 GregorianCalendar http://docs.oracle.com/javase/6/docs/api/java/util/GregorianCalendar.html

GregorianCalendar gc = new GregorianCalendar();
if (gc.isLeapYear(year) )

关于Java 有效日期闰年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18607614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com