Skip to content

Instantly share code, notes, and snippets.

@rajatdiptabiswas
Last active July 30, 2018 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajatdiptabiswas/29f3a2bc91da9585cde163508fc6b358 to your computer and use it in GitHub Desktop.
Save rajatdiptabiswas/29f3a2bc91da9585cde163508fc6b358 to your computer and use it in GitHub Desktop.
Finding the day from a given date using the Tomohiko Sakamoto Algorithm
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