beginnerLogical ThinkingJava

Find the Day of the Week for a Given Date

Given a day, month and year, return the name of the weekday using Zeller's congruence.

Quick Answer

Use Zeller's congruence. Treat January and February as months 13 and 14 of the previous year, then compute h = (q + 13*(m+1)/5 + K + K/4 + J/4 + 5*J) mod 7, where q is the day, K is year mod 100 and J is year / 100. The result h maps to a weekday, with 0 being Saturday.

Problem Statement

Given a date as three integers — day, month, and year in the Gregorian calendar — return the name of the day of the week (for example Monday).

You should compute it with arithmetic rather than relying on Java's date classes. A clean way is Zeller's congruence, a formula that maps any Gregorian date directly to a weekday. For example, 2000-01-01 is a Saturday.

Input: Three integers: day (1-31), month (1-12), and year (Gregorian, e.g. 2000).

Output: The weekday name as a string, e.g. Saturday.

Examples

Example 1
Input:  day=1, month=1, year=2000
Output: Saturday

January is treated as month 13 of 1999 in the formula, which yields h = 0 (Saturday).

Example 2
Input:  day=15, month=8, year=2023
Output: Tuesday

Zeller's congruence gives h = 3, which maps to Tuesday.

Constraints

  • The date is a valid Gregorian date
  • 1 <= month <= 12, 1 <= day <= 31, year > 0

Think Before You Code

Reveal the questions to ask yourself first
  • Why do January and February need special handling in Zeller's congruence?
  • What do the terms K (year of the century) and J (the century) represent?
  • Which weekday does the result value 0 correspond to in this formula?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Zeller's congruence treats January and February as months 13 and 14 of the *previous* year, so adjust the year down for those months.
Hint 2
Compute `h = (q + 13*(m+1)/5 + K + K/4 + J/4 + 5*J) % 7`, with integer division throughout.
Hint 3
In this form, `h = 0` is Saturday, `1` is Sunday, up to `6` for Friday — index a names array with h.

Approach

Reveal the step-by-step approach

Apply Zeller's congruence for the Gregorian calendar.

  1. Let q = day, m = month, y = year.
  2. If m < 3 (January or February), add 12 to m and subtract 1 from y, so they count as months 13 and 14 of the prior year.
  3. Let K = y % 100 (year within the century) and J = y / 100 (the century).
  4. Compute h = (q + 13*(m + 1)/5 + K + K/4 + J/4 + 5*J) % 7 using integer math.
  5. Map h to a name where index 0 is Saturday, 1 is Sunday, ..., 6 is Friday.

Every division in step 4 is integer division, which is exactly what the formula requires.

Dry Run

Walk through the example step by step

Finding the weekday for 2000-01-01:

q = 1, month = 1 -> January, so m = 13, y = 1999
K = 1999 % 100 = 99
J = 1999 / 100 = 19
13*(m+1)/5 = 13*14/5 = 182/5 = 36   (integer division)
h = (1 + 36 + 99 + 99/4 + 19/4 + 5*19) % 7
  = (1 + 36 + 99 + 24 + 4 + 95) % 7
  = 259 % 7
  = 0   -> days[0] = Saturday

Solution

Reveal the full Java solution
public class DayOfWeek {
    private static final String[] DAYS = {
        "Saturday", "Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday"
    };

    public static String dayOfWeek(int day, int month, int year) {
        int q = day;
        int m = month;
        int y = year;
        if (m < 3) {      // treat Jan/Feb as months 13/14 of previous year
            m += 12;
            y -= 1;
        }
        int k = y % 100;  // year within the century
        int j = y / 100;  // the century
        int h = (q + (13 * (m + 1)) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
        return DAYS[h];
    }

    public static void main(String[] args) {
        System.out.println(dayOfWeek(1, 1, 2000));  // Saturday
        System.out.println(dayOfWeek(15, 8, 2023)); // Tuesday
    }
}

Zeller's congruence encodes how many days the weekday shifts across months, years, and centuries. The January/February adjustment exists because the extra day of a leap year is added at the end of February, so the formula is simpler when those months are pushed to the tail of the previous year.

Because Java's % can never produce a negative result for the non-negative sum computed here, h is always a valid index from 0 to 6 into the day-name array.

Time: O(1)Space: O(1)

Common Mistakes

  • Forgetting the January/February shift, which throws off the result for early-year dates.
  • Using floating-point division instead of integer division inside the formula.

Edge Cases to Test

  • A January or February date exercises the month/year adjustment branch.
  • A leap day like 2020-02-29 (a Saturday) must be handled by the same formula.
  • Century-boundary dates such as 2000-01-01 rely on the J (century) term being correct.

Interview Follow-Ups

  • How would you validate the input date before running the formula?
  • How does this formula differ for the Julian calendar rather than the Gregorian one?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us