Search code examples
javacalendar

Java: calculating day of week works for all dates except this month


I am writing a program for my programming class, where the objective is to calculate the day of the week of a day and month entered. I must do this without using the Calendar class.

My code works with any month/year I supply, except for this month(7/2013). I can't figure out why that would be. Can anybody help with this? Examples of the outputs are as follows:

Wrong example

run:
Gregorian Calendar Creator
Enter a month: 7
Enter a year: 2013
0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat
Test this in a UNIX terminal by typing 'cal July 2013' or similar date
First day of the month: 2
First day of the year: 1
BUILD SUCCESSFUL (total time: 5 seconds)

Right Example

run:
Gregorian Calendar Creator
Enter a month: 7
Enter a year: 2012
0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat
Test this in a UNIX terminal by typing 'cal July 2013' or similar date
First day of the month: 1
First day of the year: 0
BUILD SUCCESSFUL (total time: 5 seconds)

I have linked a downloadable Java file here

package calendar;

/**
 *
 * @author Michael Dornisch
 */
import java.util.Scanner;

public class Calendar {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Initiating vars
        int inMonth;
        int inYear;

        //Initiating Scanner
        Scanner input = new Scanner(System.in);

        System.out.println("Gregorian Calendar Creator"); //Just a print line

        /* This block gets a month. Instead of exiting when the user inputs an 
         * invalid month, it instead asks again untl a proper month is used.
         */
        System.out.print("Enter a month: ");
        inMonth = input.nextInt();
        while (inMonth > 12 || inMonth <= 0) {
            System.out.println("ERROR: Improper month. Please enter a proper numerical month (1-12).");
            System.out.print("Enter a month: ");
            inMonth = input.nextInt();
        }
        /* This block gets a year. Instead of exiting when the user inputs an 
         * invalid year, it instead asks again untl a proper year is used.
         */
        System.out.print("Enter a year: ");
        inYear = input.nextInt();
        while (inYear < 1752) {
            System.out.println("ERROR: Improper Year. Please enter a year that is 1753 or greater.");
            System.out.print("Enter a year: ");
            inYear = input.nextInt();
        }
        /* Here is where I am testing everything
         * 
         */
        Calendar newCal = new Calendar();
        System.out.println("0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thu, 5=Fri, 6=Sat");
        System.out.println("Test this in a UNIX terminal by typing 'cal July 2013' or similar date");
        System.out.println("First day of the month: " + newCal.firstDayOfMonth(inMonth, inYear));
        System.out.println("First day of the year: " + newCal.firstDayOfYear(inYear));
    }

    public boolean leap(int inputYear) {

        /* This IF is checked first. If the year is divisible by 100, it isn't a 
         * leap year unless it is also divisible by 400. 
         */
        if (inputYear % 100 == 0) {
            if (inputYear % 400 == 0) {
                return (true);
            } else {
                return (false);
            }
        }
        //Any other number that's divisible by 4 is also a leap year
        if (inputYear % 4 == 0) {
            return (true);
        } else {
            return (false);
        }
    }

    public int firstDayOfYear(int inputYear) {
        /*
         * Here is a formula for finding the day of the week for ANY date.
         * N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
         * N = day of week
         * d = day of month
         * m = number of month
         * y = year
         *  mod 7 for answer
         * 0 = sunday, 1 = monday, etc.
         */
        int day = 5 + (6 / 5) + inputYear + (inputYear / 4) - (inputYear / 100) + (inputYear / 400);
        day = day % 7;
        return (day);
    }

    public int firstDayOfMonth(int inputMonth, int inputYear) {
        /*
         * Here is a formula for finding the day of the week for ANY date.
         * N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
         * N = day of week
         * d = day of month
         * m = number of month
         * y = year
         *  mod 7 for answer
         * 0 = sunday, 1 = monday, etc.
         */
        int day = 3 + (2 * inputMonth) + ((3 * (inputMonth + 1)) / 5) + inputYear + (inputYear / 4) - (inputYear / 100) + (inputYear / 400);
        day = day % 7;
        return (day);
    }
}

Solution

  • Not sure if you're formula is correct and the numbers to day matches up with it. Link

    N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2

    i would also make this method more generic

     public int firstDayOfYear(int day, int month, int year) {
            int day = day + (2*month) + ((3*(month+1))/5) + year + (year/4) - (year/100) + (year/ 400) +2;
            return day % 7;
        }
    
     System.out.println("First day of the year: " + newCal.firstDayOfYear(1, 1, inYear));