forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoomsday.java
More file actions
58 lines (54 loc) · 1.59 KB
/
Doomsday.java
File metadata and controls
58 lines (54 loc) · 1.59 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
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Determines the day of the week.
*
* @author Atom
*
*/
public class Doomsday {
/**
* Determines the day of the week using Tomohiko Sakamoto's Algorithm
* to calculate Day of Week based on Gregorian calendar.
*
* @param y year
* @param m month
* @param d day
* @return day of the week (0 = sunday, 6 = saturday)
*/
public static int dow(int y, int m, int d) {
final int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= (m < 3) ? 1 : 0;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
/**
* Determines the day of the week using Tomohiko Sakamoto's Algorithm
* to calculate Day of Week based on Gregorian calendar.
*
* @param y year
* @param m month
* @param d day
* @return day of the week
*/
public static String dowS(int y, int m, int d) {
switch (dow(y, m, d)) {
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";
default:
System.out.println("Unknown dow");
}
return null;
}
public static void main(String[] args) {
System.out.println(dow(1886, 5, 1) + ": " + dowS(1886, 5, 1));
System.out.println(dow(1948, 12, 10) + ": " + dowS(1948, 12, 10));
System.out.println(dow(2001, 1, 15) + ": " + dowS(2001, 1, 15));
System.out.println(dow(2017, 10, 10) + ": " + dowS(2017, 10, 10));
System.out.println(dow(2018, 1, 1) + ": " + dowS(2018, 1, 1));
System.out.println(dow(2018, 2, 16) + ": " + dowS(2018, 2, 16));
System.out.println(dow(2018, 5, 17) + ": " + dowS(2018, 5, 17));
}
}