-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDoomsday.java
More file actions
45 lines (33 loc) · 1.22 KB
/
Copy pathDoomsday.java
File metadata and controls
45 lines (33 loc) · 1.22 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
38
39
40
41
42
43
44
45
/** This class contains a java implementation of the doomsday algorithm */
public class Doomsday {
/** Tests the implementation */
public static void test(){
System.out.printf("Oct 13 1994 should be a Thursday: %s\n", getWeekDay(1994, 10, 13));
System.out.printf("July 2 1364 should be a Monday: %s\n", getWeekDay(1364, 7, 2));
System.out.printf("February 7 2020 should be a Friday: %s\n", getWeekDay(2020, 2, 7));
}
/** Gets the weekday of a specific date */
public static String getWeekDay(int year, int month, int day){
int[] months = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
year -= month < 3 ? 1 : 0;
int weekday = (year + year / 4 - year / 100 + year / 400 +
months[month - 1] + day) % 7;
switch(weekday){
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
return "Unknown";
}
}