Java Programs›Date & Time›Print a Monthly Calendar

Print a Monthly Calendar in Java

intermediate·  Date & Time  ·  Date & Time

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.

Input
March 2026
Output
the 1st falls under Sunday

Java Program

Java
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

March 2026 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Core Logic

The weekday of the 1st sets the number of blank cells, then the days are printed seven to a row.

How It Works
  1. 1ym.atDay(1).getDayOfWeek().getValue() is 1 for Monday up to 7 for Sunday, so subtracting 1 gives the blank cells.
  2. 2Each day is formatted to two characters plus a space, so the columns line up.
  3. 3After every seventh cell (blanks included), the row is printed and a new one started.
  4. 4stripTrailing() removes the spaces at the end of each row.
1 March 2026 is a Sunday, so six blank cells come before it and it sits alone in the first row.
💡

Key Point: Counting the blank cells in the row position — (offset + day) % 7 — is what keeps every later week aligned.

Key Concepts

YearMonthgetDayOfWeek().getValue()String.format("%2d ")String.repeat()

Approach 2: Sunday-First Calendar

Java
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

Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Core Logic

Many countries start the week on Sunday; only the blank-cell calculation changes.

How It Works
  1. 1getValue() % 7 turns Sunday (7) into 0 and keeps Monday to Saturday as 1 to 6.
  2. 2That number is the count of blanks before the 1st.
  3. 3The rest of the loop is identical.
With Sunday first, 1 March 2026 starts the very first cell.
💡

Key Point: WeekFields.of(locale).getFirstDayOfWeek() tells you which day a given country starts its week on.

Key Concepts

getValue() % 7locale conventions

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.

Learn the Concept

Related Programs