Print a Monthly Calendar in Java
Problem
A monthly calendar is a 7-column grid; the only tricky part is how many blank cells come before the 1st, which depends on its weekday.
Print the calendar for March 2026 with weeks starting on Monday.
Java Program
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.TextStyle;
import java.util.Locale;
public class PrintMonthlyCalendar {
public static void main(String[] args) {
YearMonth ym = YearMonth.of(2026, 3);
System.out.println(" " + ym.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH) + " " + ym.getYear());
System.out.println("Mo Tu We Th Fr Sa Su");
int offset = ym.atDay(1).getDayOfWeek().getValue() - 1; // Monday = 1, so Monday needs 0 blanks
StringBuilder line = new StringBuilder(" ".repeat(offset));
for (int day = 1; day <= ym.lengthOfMonth(); day++) {
line.append(String.format("%2d ", day));
if ((offset + day) % 7 == 0) { // end of a week row
System.out.println(line.toString().stripTrailing());
line.setLength(0);
}
}
if (line.length() > 0) System.out.println(line.toString().stripTrailing());
}
}Output
Core Logic
The weekday of the 1st sets the number of blank cells, then the days are printed seven to a row.
- 1
ym.atDay(1).getDayOfWeek().getValue()is 1 for Monday up to 7 for Sunday, so subtracting 1 gives the blank cells. - 2Each day is formatted to two characters plus a space, so the columns line up.
- 3After every seventh cell (blanks included), the row is printed and a new one started.
- 4
stripTrailing()removes the spaces at the end of each row.
Key Point: Counting the blank cells in the row position — (offset + day) % 7 — is what keeps every later week aligned.
Key Concepts
Approach 2: Sunday-First Calendar
import java.time.YearMonth;
import java.time.DayOfWeek;
public class PrintMonthlyCalendarSundayFirst {
public static void main(String[] args) {
YearMonth ym = YearMonth.of(2026, 3);
System.out.println("Su Mo Tu We Th Fr Sa");
DayOfWeek first = ym.atDay(1).getDayOfWeek();
int offset = first.getValue() % 7; // Sunday (7) becomes 0, Monday 1, ... Saturday 6
StringBuilder line = new StringBuilder(" ".repeat(offset));
for (int day = 1; day <= ym.lengthOfMonth(); day++) {
line.append(String.format("%2d ", day));
if ((offset + day) % 7 == 0) {
System.out.println(line.toString().stripTrailing());
line.setLength(0);
}
}
if (line.length() > 0) System.out.println(line.toString().stripTrailing());
}
}
Output
Core Logic
Many countries start the week on Sunday; only the blank-cell calculation changes.
- 1
getValue() % 7turns Sunday (7) into 0 and keeps Monday to Saturday as 1 to 6. - 2That number is the count of blanks before the 1st.
- 3The rest of the loop is identical.
Key Point: WeekFields.of(locale).getFirstDayOfWeek() tells you which day a given country starts its week on.
Key Concepts
Interview Questions
How do you print a whole year?
Loop over the 12 months with YearMonth.of(year, m) and reuse the same method for each one.
How would you highlight today's date?
Compare each day with LocalDate.now() and wrap it in markers such as brackets when it matches.