中國公歷算法

本節介紹了公歷(Gregorian calendar)的算法, 其中包括了星期的計算和閏年 的計算。

中國公歷算法不是太難,關鍵是星期值的确定。這里給出了簡單算法:

isGregorianLeapYear() 函數反回值為 True,如果輸入值為閏年:

   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;
   }

dayOfYear() 函數反回值為輸入日期在一年之中的天數:

   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;      
   }

dayOfWeek() 函數反回值為輸入日期的星期值:

   public static int dayOfWeek(int y, int m, int d) {
      int w = 1; // 公歷一年一月一日是星期一,所以起始值為星期日
      y = (y-1)%400 + 1; // 公歷星期值分部 400 年循環一次
      int ly = (y-1)/4; // 閏年次數
      ly = ly - (y-1)/100;
      ly = ly + (y-1)/400;
      int ry = y - 1 - ly; // 常年次數
      w = w + ry; // 常年星期值增一
      w = w + 2*ly; // 閏年星期值增二
      w = w + dayOfYear(y,m,d); 
      w = (w-1)%7 + 1;
      return w;
   }

Table of Contents

 序言

 中國農歷規則和日歷原理

中國年歷算法和程式

中國公歷算法

 中國農歷算法

 中國農歷計算程式

 中國農歷計算程式使用方法

 中國二百年(1901年至2100年)年歷格式說明

 中國農曆數據的更正

 中國年歷 - 1901年至1910年

 中國年歷 - 1911年至1920年

 中國年歷 - 1921年至1930年

 中國年歷 - 1931年至1940年

 中國年歷 - 1941年至1950年

 中國年歷 - 1951年至1960年

 中國年歷 - 1961年至1970年

 中國年歷 - 1971年至1980年

 中國年歷 - 1981年至1990年

 中國年歷 - 1991年至2000年

 中國年歷 - 2001年至2010年

 中國年歷 - 2011年至2020年

 中國年歷 - 2021年至2030年

 中國年歷 - 2031年至2040年

 中國年歷 - 2041年至2050年

 中國年歷 - 2051年至2060年

 中國年歷 - 2061年至2070年

 中國年歷 - 2071年至2080年

 中國年歷 - 2081年至2090年

 中國年歷 - 2091年至2100年

 參考文獻

 PDF,EPUB,以及印刷版全版