Java Programs›Date & Time›Find the Day of the Week for a Date

Find the Day of the Week for a Date in Java

beginner·  Date & Time  ·  Date & Time

Problem

Every calendar date falls on one of seven weekdays; java.time represents them with the DayOfWeek enum, numbered from Monday (1) to Sunday (7).

Print the day of the week for three dates: 11 October 2026, 1 January 2000 and 20 July 1969.

Input
2000-01-01
Output
Saturday

Java Program

Java
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.format.TextStyle; import java.util.Locale; public class DayOfWeekForDate { public static void main(String[] args) { LocalDate[] dates = { LocalDate.of(2026, 10, 11), LocalDate.of(2000, 1, 1), LocalDate.of(1969, 7, 20)}; for (LocalDate d : dates) { DayOfWeek day = d.getDayOfWeek(); System.out.println(d + " is a " + day.getDisplayName(TextStyle.FULL, Locale.ENGLISH) + " (day " + day.getValue() + " of the week)"); // Monday = 1 ... Sunday = 7 } } }

Output

2026-10-11 is a Sunday (day 7 of the week) 2000-01-01 is a Saturday (day 6 of the week) 1969-07-20 is a Sunday (day 7 of the week)

Core Logic

getDayOfWeek() computes the weekday from the date, and getDisplayName turns the enum into readable text.

How It Works
  1. 1Each LocalDate is created with LocalDate.of.
  2. 2getDayOfWeek() returns a DayOfWeek constant such as SUNDAY.
  3. 3getDisplayName(TextStyle.FULL, Locale.ENGLISH) gives Sunday; TextStyle.SHORT would give Sun.
  4. 4getValue() gives the ISO number, from Monday = 1 to Sunday = 7.
20 July 1969, the date of the first Moon landing, was a Sunday.
💡

Key Point: ISO numbering starts the week on Monday; if you need Sunday-first numbering, use WeekFields.SUNDAY_START rather than adjusting the numbers by hand.

Key Concepts

getDayOfWeek()DayOfWeekgetDisplayName()TextStyle

Approach 2: Zeller's Congruence (No Library)

Java
public class DayOfWeekZeller { public static void main(String[] args) { System.out.println("2026-10-11 is a " + dayName(2026, 10, 11)); System.out.println("2000-01-01 is a " + dayName(2000, 1, 1)); } // Zeller's congruence for the Gregorian calendar static String dayName(int year, int month, int day) { if (month < 3) { // January and February count as months 13 and 14 of the previous year month += 12; year -= 1; } int k = year % 100; // year within the century int j = year / 100; // the century int h = (day + 13 * (month + 1) / 5 + k + k / 4 + j / 4 + 5 * j) % 7; String[] names = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; return names[h]; // h = 0 is Saturday in Zeller's numbering } }

Output

2026-10-11 is a Sunday 2000-01-01 is a Saturday

Core Logic

Zeller's congruence computes the weekday with pure arithmetic — a popular interview exercise that shows how calendar maths works.

How It Works
  1. 1January and February are treated as months 13 and 14 of the previous year, which moves the leap day to the end of the year.
  2. 2k is the year within the century and j is the century.
  3. 3The formula combines day, month, k, k/4, j/4 and 5j, then takes the result modulo 7.
  4. 4Zeller numbers the result from 0 = Saturday, so a lookup array maps it to a name.
For 1 January 2000 the month becomes 13 of 1999, and the formula gives h = 0, which is Saturday.
💡

Key Point: Integer division is essential here — every / in the formula must truncate, which is exactly what Java's int division does.

Key Concepts

Zeller's congruenceinteger divisionmodulo

Interview Questions

How do you find the next Monday after a date?

date.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).

How do you check whether a date falls on a weekend?

DayOfWeek d = date.getDayOfWeek(); boolean weekend = d == DayOfWeek.SATURDAY || d == DayOfWeek.SUNDAY;

Learn the Concept

Related Programs