-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfDaysInMonth
51 lines (41 loc) · 1.25 KB
/
NumberOfDaysInMonth
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
public class NumberOfDaysInMonth {
public static boolean isLeapYear(int year) {
boolean leapYear = false;
if (year < 1 || year > 9999) {
return leapYear;
} else {
if ((year % 4) == 0 && (year % 100 != 0)) {
leapYear = true;
} else if ((year % 100) == 0 && (year % 400 ) == 0 ) {
leapYear = true;
} else
leapYear = false;
}
return leapYear;
}
public static int getDaysInMonth(int month, int year) {
if (month < 1 || month > 12 || year < 1 || year > 9999) {
return -1;
}
int noOfDays = 0;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10 : case 12:
noOfDays = 31;
break;
case 2:
if(isLeapYear(year)) {
noOfDays = 29;
break;
}
noOfDays = 28;
break;
case 4: case 6: case 9 : case 11:
noOfDays = 30;
break;
default:
noOfDays = -1;
break;
}
return noOfDays;
}
}