INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of Day.java.


/*************************************************************************
 *  Compilation:  javac Day.java
 *  Execution:    java Day M D Y
 *  
 *  Read in the month (M), day (D), and year (Y) and print out what
 *  day of the week it falls on according to the Gregorian calendar.
 *  For M use 1 for January, 2 for February, and so forth. Outputs
 *  0 for Sunday, 1 for Monday, and so forth.
 *
 *        y = Y - (14 - M) / 12
 *        x = Y + Y/4 - Y/100 + Y/400
 *        m = M + 12 * ((14 - M) / 12) - 2
 *        d = (D + x + (31*m)/12) mod 7
 *
 *
 *  % java Day 8 2 1953            // August 2, 1953
 *  0                              // Sunday
 *
 *  % java Day 1 1 2000            // January 1, 2000
 *  6                              // Monday
 *
 *************************************************************************/

public class Day { 
   public static void main(String[] args) { 
      int M = Integer.parseInt(args[0]);
      int D = Integer.parseInt(args[1]);
      int Y = Integer.parseInt(args[2]);

      int y = Y - (14 - M) / 12;
      int x = y + y/4 - y/100 + y/400;
      int m = M + 12 * ((14 - M) / 12) - 2;
      int d = (D + x + (31*m)/12) % 7;

      System.out.println(d);
   }

}


Last updated: Wed Feb 11 18:07:23 EST 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.