请使用 java 语言,生成一个计算一个月有多少天的函数
时间: 2024-05-27 11:11:00 浏览: 31
public static int getDaysInMonth(int year, int month) {
int daysInMonth = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysInMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
default:
System.out.println("Invalid month.");
break;
}
return daysInMonth;
}
// 调用方式
int days = getDaysInMonth(2021, 2); // 返回28或29