-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfDaysInMonth.java
74 lines (55 loc) · 1.69 KB
/
NumberOfDaysInMonth.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package Chapter2.src;
public class NumberOfDaysInMonth {
public static void main(String[] args) {
System.out.println(getDaysInMonth(2, -2000));
System.out.println(getDaysInMonth(2, 2000));
System.out.println(getDaysInMonth(3, 1960));
System.out.println(getDaysInMonth(2, -2000));
}
public static boolean isLeapYear(int year) {
if (year >= 1 && year <= 9999) {
if (year % 4 != 0) {
return false;
} else if (year % 100 != 0) {
return true;
} else if (year % 400 == 0) {
return true;
}
}
return false;
}
public static int getDaysInMonth(int month, int year) {
if ((month > 1 && month < 12) && (year > 1 && year < 9999)) {
if (isLeapYear(year) && month == 2) {
return 29;
}
switch (month) {
case 1:
return 31;
case 2:
return 28;
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 30;
case 11:
return 30;
case 12:
return 31;
}
}
return -1;
}
}