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 Year.java
More file actions
37 lines (33 loc) · 1.1 KB
/
Copy pathDay of the Year.java
File metadata and controls
37 lines (33 loc) · 1.1 KB
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
class Solution {
List<Integer> daysInMonth = Arrays.asList(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
public int dayOfYear(String date) {
int year = Integer.parseInt(date.split("-")[0]);
int month = Integer.parseInt(date.split("-")[1]);
int day = Integer.parseInt(date.split("-")[2]);
int numOfDays = getNumOfDays(month, isLeapYear(year));
return numOfDays + day;
}
private boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0;
}
else {
return true;
}
}
return false;
}
private int getNumOfDays(int month, boolean isLeapYear) {
int numOfDays = 0;
for (int i = 1; i < month; i++) {
if (i == 2) {
numOfDays += isLeapYear ? daysInMonth.get(i - 1) + 1 : daysInMonth.get(i - 1);
}
else {
numOfDays += daysInMonth.get(i - 1);
}
}
return numOfDays;
}
}