Skip to content

Instantly share code, notes, and snippets.

@SpencerBingol
Last active August 29, 2015 13:57
Show Gist options
  • Save SpencerBingol/9605687 to your computer and use it in GitHub Desktop.
Save SpencerBingol/9605687 to your computer and use it in GitHub Desktop.
[Java] Zeller's Congruence Implementation, finds Day of Week given numeric month, day of month, and full year.
/************************************************
* An implementation of Zeller's Congruence - presuming MM/DD/YYYY input
* more information here:
* http://en.wikipedia.org/wiki/Zeller's_congruence
*
* TL;DR notes:
* - january and february are treated as months 13 & 14 of the previous year
* - resulting day number begins on saturday (0 = "Sat", 1 = "Sun", ... , 6 = "Fri")
* - actual formula listed below
* ********************************************/
private void zeller (int mon, int day, int yr) {
private String days[] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
int cenYr = yr % 100;
int cen = yr / 100;
if (mon <= 2) {
yr -= 1;
mon += 12;
}
/****************************************
* ALGORITHM:
* weekDay = (day + [13(month + 1)/5] + year of century + [year of century/4] + [century/4] - [2*century]) MOD 7
* ************************************/
int weekDay = (day + ( ( 13*(mon+1) )/5 ) + cenYr + (cenYr/4) + (cen/4) - (2*cen) );
weekDay %= 7;
return days[weekDay];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment