forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay of the Week.java
More file actions
30 lines (30 loc) · 817 Bytes
/
Day of the Week.java
File metadata and controls
30 lines (30 loc) · 817 Bytes
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
class Solution {
int[] m = {0,31,28,31,30,31,30,31,31,30,31,30,31};
String[] res = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
public String dayOfTheWeek(int day, int month, int year) {
int days = years(year);
if(isLeap(year))
m[2] = 29;
for(int i=0; i < month; i++){
days += m[i];
}
days += day-1;
return res[days%7];
}
private int years(int y){
int count = 0;
for(int i=1971; i < y; i++){
if(isLeap(i))
count += 366;
else
count += 365;
}
return count;
}
private boolean isLeap(int y){
if(y % 4 != 0) return false;
else if(y%100 != 0) return true;
else if(y % 400 != 0) return false;
else return true;
}
}