Determining Day of the week using Zeller’s Congruence in Java

int k = year % 7; // The year of the century

You might want % 100 there.

Also, you’re mixing two of the formulae. If you’re after the best implementation in software (as provided by Wikipedia), try this:

// ...
// remove j and k
int y = year;
// ...
// reformatted for readability
int h = (q +
         (int)((26 * (m + 1)) / 10.0) +
         y +
         (int)(y / 4.0) +
//       changes after here
         6 * (int)(y / 100.0) +
         (int)(y / 400.0))
        % 7;

or this:

int h = (q + (int)((13 * (m + 1)) / 5.0) + k + (int)(k / 4.0)
//                  ^^
           + (int)(j / 4.0) + (5 * j)) % 7;

You were (likely inadvertently) using the start of the second software formula with the end of the first one, causing the computer no end of confusion.

Leave a Comment