user9587342
user9587342

Reputation:

How do i get the first day of the month by using java calendar?

I have written a program that is taking input from the user stating the year and the month, and am trying to print out the month. I can get the month to print and my spacing is working. However, I cannot get the days to work. The first day of the month is right for January 2018, but it isn't right when I do it for a different year or later month. I have to use the java package calendar. I have printed my code below is there something wrong with my code? Is there any way to fix it?

import java.util.Calendar;
import.java.util.Scanner;
public class MonthCalendar {
  public static void main(String[] args) {
    int year; // year
    int startDayOfMonth;
    int spaces;
    int month;

    //Creates a new Scanner
    Scanner scan = new Scanner(System.in);

    //Prompts user to enter year
    System.out.println("Enter a year: ");
    year = scan.nextInt();

    //Prompts user to enter month
    System.out.println("Enter the number of the month: ");
    month = scan.nextInt();


    //Calculates the 1st day of that month
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, 1);
    int day = cal.get(Calendar.DAY_OF_WEEK) - 1;

    // months[i] = name of month i
    String[] months = {
      " ",
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
    };

    // days[i] = number of days in month i
    int[] days = {
      0,
      31,
      28,
      31,
      30,
      31,
      30,
      31,
      31,
      30,
      31,
      30,
      31
    };


    // check for leap year
    if ((((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) && month == 2)
      days[month] = 29;


    // print calendar header
    // Display the month and year
    System.out.println("              " + months[month] + " " + year);

    // Display the lines
    System.out.println("___________________________________________");
    System.out.println("  Sun   Mon   Tue   Wed   Thu   Fri   Sat");

    // spaces required
    spaces = (days[month + 1] + day) % 7;

    // print the calendar
    for (int i = 0; i < spaces; i++)
      System.out.print("      ");
    for (int i = 1; i <= days[month]; i++) {
      System.out.printf(" %4d ", i);
      if (((i + spaces) % 7 == 0) || (i == days[month])) System.out.println();
    }

    System.out.println();

  }

Upvotes: 1

Views: 1126

Answers (1)

Isac
Isac

Reputation: 1874

As pointed out in the comments already your problem is not in the date calculation itself but in the way you set the spaces initially:

spaces = (days[month+1] + day )%7;

That should instead be:

spaces = day;

You only need to know the weekday you're at, to know how far you must move in the first week regarding spaces. So if you are in a Sunday you advance 0 spaces, but if you are on Tuesday you want do advance 2 spaces, and so on and so forth. In the end you advance as many spaces as your starting weekday, which is what the day variable holds.

Take a look at the code giving the proper output for February 2018 in Ideone

Which yields the following output:

Enter a year: 
2018
Enter the number of the month: 
2
              February 2018
___________________________________________
  Sun   Mon   Tue   Wed   Thu   Fri   Sat
                            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 

Upvotes: 0

Related Questions