Last active
July 30, 2018 18:03
-
-
Save rajatdiptabiswas/29f3a2bc91da9585cde163508fc6b358 to your computer and use it in GitHub Desktop.
Finding the day from a given date using the Tomohiko Sakamoto Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class Sakamoto { | |
static int sakamotoAlgorithm(int date, int month, int year) { | |
int[] dateDiff = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; | |
if (month < 3) | |
year -= 1; | |
return (year + year/4 - year/100 + year/400 + dateDiff[month-1] + date) % 7; | |
} | |
public static void main (String[] args) { | |
Scanner scan = new Scanner(System.in); | |
Integer date = scan.nextInt(); | |
Integer month = scan.nextInt(); | |
Integer year = scan.nextInt(); | |
int day = sakamotoAlgorithm(date, month, year); | |
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; | |
System.out.println(days[day]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment