-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaysInMonth.java
More file actions
23 lines (21 loc) · 774 Bytes
/
Copy pathDaysInMonth.java
File metadata and controls
23 lines (21 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class DaysInMonth {
public static void main(String[] args) {
int month = 2, year = 2024;
System.out.println("Days in month " + month + " of " + year + " = " + daysInMonth(month, year));
}
static int daysInMonth(int month, int year) {
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
throw new IllegalArgumentException("Invalid month: " + month);
}
}
static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}