Find the Day of the Week for a Date in Java
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.
Java Program
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
Core Logic
getDayOfWeek() computes the weekday from the date, and getDisplayName turns the enum into readable text.
- 1Each
LocalDateis created withLocalDate.of. - 2
getDayOfWeek()returns aDayOfWeekconstant such asSUNDAY. - 3
getDisplayName(TextStyle.FULL, Locale.ENGLISH)givesSunday;TextStyle.SHORTwould giveSun. - 4
getValue()gives the ISO number, from Monday = 1 to Sunday = 7.
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
Approach 2: Zeller's Congruence (No Library)
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
Core Logic
Zeller's congruence computes the weekday with pure arithmetic — a popular interview exercise that shows how calendar maths works.
- 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
kis the year within the century andjis the century. - 3The formula combines day, month,
k,k/4,j/4and5j, then takes the result modulo 7. - 4Zeller numbers the result from 0 = Saturday, so a lookup array maps it to a name.
Key Point: Integer division is essential here — every / in the formula must truncate, which is exactly what Java's int division does.
Key Concepts
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;