-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfDaysAndWeekPerMonth.java
167 lines (147 loc) · 3.99 KB
/
NumberOfDaysAndWeekPerMonth.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.javamultiplex.datetime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
//How to find number of days and weeks per month?
public class NumberOfDaysAndWeekPerMonth {
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter month name : ");
String monthName = input.next();
String year = null;
int[] weeksAndRemainingDays = new int[2];
int days = 0;
// Converting String to uppercase
monthName = monthName.toUpperCase();
if (monthName.equals("FEBRUARY") || monthName.equals("FEB")) {
System.out.println("Enter Year : ");
year = input.next();
}
if (isValidMonth(monthName)) {
if ((monthName.equals("FEBRUARY") || monthName.equals("FEB"))) {
if (isValidYear(year)) {
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
System.out.println("Number of days in " + monthName
+ " " + year + " are : " + days);
weeksAndRemainingDays = getWeeksAndRemainingDays(days);
System.out.println("Number of weeks in " + monthName
+ " " + year + " are : "
+ weeksAndRemainingDays[0] + " Weeks + "
+ weeksAndRemainingDays[1] + " Day/s");
} else {
System.out.println("Year should be 4 digit long.");
}
} else {
days = getNumberofDaysByMonthName(monthName);
System.out.println("Number of days in " + monthName
+ " are : " + days);
weeksAndRemainingDays = getWeeksAndRemainingDays(days);
System.out
.println("Number of weeks in " + monthName
+ " are : " + weeksAndRemainingDays[0]
+ " Weeks + " + weeksAndRemainingDays[1]
+ " Day/s");
}
} else {
System.out.println("Month name is not valid.");
}
} finally {
if (input != null) {
input.close();
}
}
}
private static int[] getWeeksAndRemainingDays(int days) {
int weeks[] = new int[2];
weeks[0] = days / 7;
weeks[1] = days % 7;
return weeks;
}
private static int getNumberofDaysByMonthName(String monthName) {
int days = 0;
switch (monthName) {
case "JANUARY":
case "JAN":
days = 31;
break;
case "MARCH":
case "MAR":
days = 31;
break;
case "APRIL":
case "APR":
days = 30;
break;
case "MAY":
days = 31;
break;
case "JUNE":
days = 30;
break;
case "JULY":
days = 31;
break;
case "AUGUST":
case "AUG":
days = 31;
break;
case "SEPTEMBER":
case "SEP":
days = 30;
break;
case "OCTOBER":
case "OCT":
days = 31;
break;
case "NOVEMBER":
case "NOV":
days = 30;
break;
case "DECEMBER":
case "DEC":
days = 31;
break;
}
return days;
}
private static boolean isLeapYear(String year) {
boolean result = false;
// Converting String to int.
int myYear = Integer.parseInt(year);
if ((myYear % 4 == 0 && myYear % 100 != 0) || myYear % 400 == 0) {
result = true;
}
return result;
}
private static boolean isValidMonth(String monthName) {
boolean result = false;
String[] fullMonthNames = { "JANUARY", "FEBRUARY", "MARCH", "APRIL",
"MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",
"NOVEMBER", "DECEMBER" };
String[] halfMonthNames = { "JAN", "FEB", "MAR", "APR", "AUG", "SEP",
"OCT", "NOV", "DEC" };
// Converting Array to List
List<String> fullMonths = new ArrayList<>(Arrays.asList(fullMonthNames));
List<String> halfMonths = new ArrayList<>(Arrays.asList(halfMonthNames));
if (fullMonths.contains(monthName) || halfMonths.contains(monthName)) {
result = true;
}
return result;
}
private static boolean isValidYear(String year) {
boolean result = false;
// Regular expression that matches a String contains 4 digits.
String pattern = "[0-9]{4}";
if (year.matches(pattern)) {
result = true;
}
return result;
}
}