please some one help me with this code

Dani AI

Generated

— quick, practical answer that builds on what and wrote.

As hinted, you need a date/time API. For new code prefer the modern java.time API (Java 8+): it makes month boundaries and day-of-week calculations simple and less error-prone than the old Date/Calendar classes. The snippet below prints the current year’s calendar, month by month, with weeks starting on Sunday.

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.TextStyle;
import java.util.Locale;

public class YearCalendar {
    public static void main(String[] args) {
        int year = LocalDate.now().getYear();
        printYear(year);
    }

    static void printYear(int year) {
        for (int m = 1; m <= 12; m++) {
            YearMonth ym = YearMonth.of(year, m);
            String monthName = ym.getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault());
            System.out.println("  " + monthName + " " + year);
            System.out.println("Sun Mon Tue Wed Thu Fri Sat");

            int indent = ym.atDay(1).getDayOfWeek().getValue() % 7; // Sunday -> 0
            for (int i = 0; i < indent; i++) System.out.print("    ");

            for (int d = 1; d <= ym.lengthOfMonth(); d++) {
                System.out.printf("%3d ", d);
                if ((indent + d) % 7 == 0) System.out.println();
            }
            System.out.println("\n");
        }
    }
}

Notes and troubleshooting:

  • The code uses YearMonth and LocalDate to avoid manual leap-year or month-length logic.
  • It assumes Sunday-first weeks. To adapt to locale-first-week or Monday-first, compute the offset with WeekFields.of(Locale.getDefault()).getFirstDayOfWeek() instead of hard-coding Sunday.
  • If running on Java 7 or earlier, use Calendar or a backport (or upgrade to Java 8+). For more on java.time, see the official Java date/time tutorial and the LocalDate javadoc:

Recommended Answers

All 2 Replies

Try Google. Or any other search.

Use the classes java.util.Date with the java.textSimpleDateformatter, or/and java.util.Calendar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.