The Gregorian Calendar Algorithm

This section describes the algorithm to calculate the day of the week based on leap year rules. The algorithm can be implemented with isGregorianLeapYear(), dayOfYear() and dayOfWeek() functions.

The Gregorian calendar is not so difficult to calculate. Here is a simple algorithm to determine the day of the week for a given Gregorian date, month and year:

The isGregorianLeapYear() function returns true if the given year is a leap year:

   public static boolean isGregorianLeapYear(int year) {
      boolean isLeap = false;
      if (year%4==0) isLeap = true;
      if (year%100==0) isLeap = false;
      if (year%400==0) isLeap = true;
      return isLeap;
   }

The dayOfYear() function returns the day of the year for any given year, month and day:

   public static int dayOfYear(int y, int m, int d) {
      int c = 0;
      for (int i=1; i<m; i++) { // Number of months passed
         c = c + daysInGregorianMonth(y,i);
      }
      c = c + d;
      return c;      
   }
   public static int daysInGregorianMonth(int y, int m) {
      int d = daysInGregorianMonth[m-1];
      if (m==2 && isGregorianLeapYear(y)) d++;
      return d;      
   }

The dayOfWeek() function returns the day of the week for any given year, month and day:

   public static int dayOfWeek(int y, int m, int d) {
      int w = 1; // 01-Jan-0001 is Monday, so base is Sunday
      y = (y-1)%400 + 1; // Gregorian calendar cycle is 400 years
      int ly = (y-1)/4; // Leap years passed
      ly = ly - (y-1)/100; // Adjustment
      ly = ly + (y-1)/400; // Adjustment
      int ry = y - 1 - ly; // Regular years passed
      w = w + ry; // Regular year has one extra week day
      w = w + 2*ly; // Leap year has two extra week days
      w = w + dayOfYear(y,m,d); 
      w = (w-1)%7 + 1;
      return w;
   }

Table of Contents

 About This Book

 Chinese Calendar Background Information

Chinese Calendar Algorithm and Program

The Gregorian Calendar Algorithm

 The Chinese Calendar Algorithm

 Chinese Calendar Program in java

 How to Use Chinese Calendar Program

 Chinese Calendar Format and Notations

 Chinese Calendars: Year 1901 to 1910

 Chinese Calendars: Year 1911 to 1920

 Chinese Calendars: Year 1921 to 1930

 Chinese Calendars: Year 1931 to 1940

 Chinese Calendars: Year 1941 to 1950

 Chinese Calendars: Year 1951 to 1960

 Chinese Calendars: Year 1961 to 1970

 Chinese Calendars: Year 1971 to 1980

 Chinese Calendars: Year 1981 to 1990

 Chinese Calendars: Year 1991 to 2000

 Chinese Calendars: Year 2001 to 2010

 Chinese Calendars: Year 2011 to 2020

 Chinese Calendars: Year 2021 to 2030

 Chinese Calendars: Year 2031 to 2040

 Chinese Calendars: Year 2041 to 2050

 Chinese Calendars: Year 2051 to 2060

 Chinese Calendars: Year 2061 to 2070

 Chinese Calendars: Year 2071 to 2080

 Chinese Calendars: Year 2081 to 2090

 Chinese Calendars: Year 2091 to 2100

 References

 PDF Printing Version