Skip to content

Commit 93c8c52

Browse files
authored
Create Doomsday.java
1 parent 29252ba commit 93c8c52

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Doomsday/Doomsday.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Determines the day of the week.
3+
*
4+
* @author Atom
5+
*
6+
*/
7+
public class Doomsday {
8+
9+
/**
10+
* Determines the day of the week using Tomohiko Sakamoto's Algorithm
11+
* to calculate Day of Week based on Gregorian calendar.
12+
*
13+
* @param y year
14+
* @param m month
15+
* @param d day
16+
* @return day of the week (0 = sunday, 6 = saturday)
17+
*/
18+
public static int dow(int y, int m, int d) {
19+
final int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
20+
y -= (m < 3) ? 1 : 0;
21+
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
22+
}
23+
24+
/**
25+
* Determines the day of the week using Tomohiko Sakamoto's Algorithm
26+
* to calculate Day of Week based on Gregorian calendar.
27+
*
28+
* @param y year
29+
* @param m month
30+
* @param d day
31+
* @return day of the week
32+
*/
33+
public static String dowS(int y, int m, int d) {
34+
switch (dow(y, m, d)) {
35+
case 0: return "Sunday";
36+
case 1: return "Monday";
37+
case 2: return "Tuesday";
38+
case 3: return "Wednesday";
39+
case 4: return "Thursday";
40+
case 5: return "Friday";
41+
case 6: return "Saturday";
42+
default:
43+
System.out.println("Unknown dow");
44+
}
45+
return null;
46+
}
47+
48+
public static void main(String[] args) {
49+
System.out.println(dow(1886, 5, 1) + ": " + dowS(1886, 5, 1));
50+
System.out.println(dow(1948, 12, 10) + ": " + dowS(1948, 12, 10));
51+
System.out.println(dow(2001, 1, 15) + ": " + dowS(2001, 1, 15));
52+
System.out.println(dow(2017, 10, 10) + ": " + dowS(2017, 10, 10));
53+
System.out.println(dow(2018, 1, 1) + ": " + dowS(2018, 1, 1));
54+
System.out.println(dow(2018, 2, 16) + ": " + dowS(2018, 2, 16));
55+
System.out.println(dow(2018, 5, 17) + ": " + dowS(2018, 5, 17));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)