中国农历二百年算法及年历程序分析二

      // 注意:闰月 m < 0

字串9

      int index = y - baseChineseYear + baseIndex; 字串7

      int v = 0; 字串5

      int l = 0; 字串1

      int d = 30;

字串3

      if (1<=m && m<=8) { 字串2

         v = chineseMonths[2*index]; 字串4

         l = m - 1;

字串2

         if ( ((v>>l)&0x01)==1 ) d = 29;

字串5

      } else if (9<=m && m<=12) { 字串2

         v = chineseMonths[2*index+1];

字串7

         l = m - 9;

字串6

         if ( ((v>>l)&0x01)==1 ) d = 29; 字串3

      } else { 字串5

         v = chineseMonths[2*index+1];

字串3

         v = (v>>4)&0x0F; 字串8

         if (v!=Math.abs(m)) {

字串3

            d = 0; 字串9

         } else { 字串8

            d = 29; 字串1

            for (int i=0; i

字串8

               if (bigLeapMonthYears[i]==index) { 字串5

                  d = 30;

字串2

                  break;

字串2

               }

字串3

            }

字串9

         } 字串5

      } 字串9

      return d;

字串8

   } 字串7

   public static int nextChineseMonth(int y, int m) {

字串2

      int n = Math.abs(m) + 1; 字串7

      if (m>0) {

字串9

         int index = y - baseChineseYear + baseIndex;

字串5

         int v = chineseMonths[2*index+1]; 字串7

         v = (v>>4)&0x0F;

字串8

         if (v==m) n = -m;

字串2

      }

字串2

      if (n==13) n = 1; 字串1

      return n; 字串2

   }

字串4

   private static char[][] sectionalTermMap = {

字串9

   {7,6,6,6,6,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,5,5,5,5,5,4,5,5}, 字串8

   {5,4,5,5,5,4,4,5,5,4,4,4,4,4,4,4,4,3,4,4,4,3,3,4,4,3,3,3},

字串8

   {6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5},

字串3

   {5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,4,4,5,5,4,4,4,5,4,4,4,4,5},

字串6

   {6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5},

字串4

   {6,6,7,7,6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5}, 字串1

   {7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,6,6,6,7,7}, 字串9

   {8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,7},

字串9

   {8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,7}, 字串6

   {9,9,9,9,8,9,9,9,8,8,9,9,8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,8},

字串9

   {8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,7},

字串7

   {7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,6,6,6,7,7}

字串4

   };

字串6

   private static char[][] sectionalTermYear = {

字串6

   {13,49,85,117,149,185,201,250,250},

字串8

   {13,45,81,117,149,185,201,250,250}, 字串9

   {13,48,84,112,148,184,200,201,250}, 字串9

   {13,45,76,108,140,172,200,201,250}, 字串7

   {13,44,72,104,132,168,200,201,250}, 字串8

   {5 ,33,68,96 ,124,152,188,200,201}, 字串1

   {29,57,85,120,148,176,200,201,250}, 字串6

   {13,48,76,104,132,168,196,200,201}, 字串1

   {25,60,88,120,148,184,200,201,250}, 字串8

   {16,44,76,108,144,172,200,201,250}, 字串9

   {28,60,92,124,160,192,200,201,250},

字串5

   {17,53,85,124,156,188,200,201,250}

字串3

   }; 字串3

   private static char[][] principleTermMap = { 字串7

   {21,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20,20,20,20,20,20,19,

字串1

      20,20,20,19,19,20}, 字串3

   {20,19,19,20,20,19,19,19,19,19,19,19,19,18,19,19,19,18,18,19,19,18,

字串3

      18,18,18,18,18,18},

字串3

   {21,21,21,22,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20,21,20,20, 字串2

      20,20,19,20,20,20,20},

字串7

   {20,21,21,21,20,20,21,21,20,20,20,21,20,20,20,20,19,20,20,20,19,19, 字串8

      20,20,19,19,19,20,20}, 字串8

   {21,22,22,22,21,21,22,22,21,21,21,22,21,21,21,21,20,21,21,21,20,20,

字串5

      21,21,20,20,20,21,21}, 字串3

   {22,22,22,22,21,22,22,22,21,21,22,22,21,21,21,22,21,21,21,21,20,21, 字串4

      21,21,20,20,21,21,21},

字串3

   {23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23,23,22,22, 字串2

      22,23,22,22,22,22,23},

字串5

   {23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,

字串9

      23,23,22,22,22,23,23}, 字串3

   {23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,

字串2

      23,23,22,22,22,23,23}, 字串4

   {24,24,24,24,23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,

字串4

      23,23,22,22,23,23,23}, 字串4

   {23,23,23,23,22,23,23,23,22,22,23,23,22,22,22,23,22,22,22,22,21,22,

字串2

      22,22,21,21,22,22,22}, 字串6

   {22,22,23,23,22,22,22,23,22,22,22,22,21,22,22,22,21,21,22,22,21,21,

字串4

      21,22,21,21,21,21,22}

字串7

   }; 字串5

   private static char[][] principleTermYear = { 字串4

   {13,45,81,113,149,185,201}, 字串7

   {21,57,93,125,161,193,201}, 字串5

   {21,56,88,120,152,188,200,201},

字串9

   {21,49,81,116,144,176,200,201}, 字串8

   {17,49,77,112,140,168,200,201},

字串2

   {28,60,88,116,148,180,200,201},

字串5

   {25,53,84,112,144,172,200,201}, 字串9

   {29,57,89,120,148,180,200,201},

字串1

   {17,45,73,108,140,168,200,201},

字串4

   {28,60,92,124,160,192,200,201},

字串7

   {16,44,80,112,148,180,200,201}, 字串9

   {17,53,88,120,156,188,200,201}

字串4

   };

字串5

   public int computeSolarTerms() {

字串2

      if (gregorianYear<1901 || gregorianYear>2100) return 1; 字串9

      sectionalTerm = sectionalTerm(gregorianYear, gregorianMonth);

字串8

      principleTerm = principleTerm(gregorianYear, gregorianMonth);

字串2

      return 0;

字串3

   }

字串2

   public static int sectionalTerm(int y, int m) {

字串3

      if (y<1901 || y>2100) return 0;

字串7

      int index = 0;

字串6

      int ry = y-baseYear+1;

字串7

      while (ry>=sectionalTermYear[m-1][index]) index++;

字串5

      int term = sectionalTermMap[m-1][4*index+ry%4];

字串4

      if ((ry == 121)&&(m == 4)) term = 5; 字串7

      if ((ry == 132)&&(m == 4)) term = 5; 字串1

      if ((ry == 194)&&(m == 6)) term = 6; 字串2

      return term;

字串1

   }

字串1

   public static int principleTerm(int y, int m) { 字串4

      if (y<1901 || y>2100) return 0; 字串4

      int index = 0; 字串8

      int ry = y-baseYear+1; 字串5

      while (ry>=principleTermYear[m-1][index]) index++; 字串9

      int term = principleTermMap[m-1][4*index+ry%4]; 字串1

      if ((ry == 171)&&(m == 3)) term = 21;

字串5

      if ((ry == 181)&&(m == 5)) term = 21;

字串7

      return term; 字串8

   }

字串8

   public String toString() { 字串7

      StringBuffer buf = new StringBuffer();

字串1

      buf.append("Gregorian Year: "+gregorianYear+"\n"); 字串1

      buf.append("Gregorian Month: "+gregorianMonth+"\n");

字串5

      buf.append("Gregorian Date: "+gregorianDate+"\n");

字串3

      buf.append("Is Leap Year: "+isGregorianLeap+"\n"); 字串5

      buf.append("Day of Year: "+dayOfYear+"\n");

字串2

      buf.append("Day of Week: "+dayOfWeek+"\n");

字串3

      buf.append("Chinese Year: "+chineseYear+"\n");

字串4

      buf.append("Heavenly Stem: "+((chineseYear-1)%10)+"\n"); 字串1

      buf.append("Earthly Branch: "+((chineseYear-1)%12)+"\n");

字串2

      buf.append("Chinese Month: "+chineseMonth+"\n"); 字串7

      buf.append("Chinese Date: "+chineseDate+"\n");

字串3

      buf.append("Sectional Term: "+sectionalTerm+"\n"); 字串6

      buf.append("Principle Term: "+principleTerm+"\n");

字串6

      return buf.toString(); 字串2

   }

字串9

   public String[] getYearTable() { 字串3

      setGregorian(gregorianYear,1,1);

字串2

      computeChineseFields(); 字串9

      computeSolarTerms();

字串9

      String[] table = new String[58]; // 6*9 + 4

字串2

      table[0] = getTextLine(27, "公历年历:"+gregorianYear); 字串4

      table[1] = getTextLine(27, "农历年历:"+(chineseYear+1)

字串7

         + " ("+stemNames[(chineseYear+1-1)%10]

字串1

         + branchNames[(chineseYear+1-1)%12] 字串5

         + " - "+animalNames[(chineseYear+1-1)%12]+")"); 字串2

      int ln = 2;

字串5

      String blank  = "                                         " 字串5

              +"  " + "                                         "; 字串8

      String[] mLeft = null;

字串4

      String[] mRight = null;

字串1

      for (int i=1; i<=6; i++) { 字串1

         table[ln] = blank; 字串5

         ln++;

字串5

         mLeft = getMonthTable(); 字串6

         mRight = getMonthTable(); 字串6

         for (int j=0; j

字串9

            String line = mLeft[j] + "  " + mRight[j];

字串5

            table[ln] = line;

字串7

            ln++; 字串4

         } 字串6

      }

字串2

      table[ln] = blank; 字串4

      ln++;

字串7

      table[ln] = getTextLine(0,

字串3

         "##/## - 公历日期/农历日期,(*)# - ()农历月第一天"); 字串2

      ln++; 字串6

      return table; 字串8

   } 字串1

   public static String getTextLine(int s, String t) { 字串7

      String str  = "                                         "

字串7

              +"  " + "                                         ";

字串9

      if (t!=null && s 字串2

         str = str.substring(0,s) + t + str.substring(s+t.length());

字串6

      return str;

字串3

   } 字串7

   private static String[] monthNames =

字串2

      {"","","","","","","","","","","十一","十二"}; 字串1

   public String[] getMonthTable() { 字串6

      setGregorian(gregorianYear,gregorianMonth,1); 字串1

      computeChineseFields(); 字串1

      computeSolarTerms();

字串1

      String[] table = new String[8];

字串7

      String title  = null; 字串3

      if (gregorianMonth<11) title  = "                   ";

字串1

      else title  = "                 ";

字串1

      title = title + monthNames[gregorianMonth-1] + ""

字串3

                    + "                   "; 字串3

      String header = "                           ";

字串5

      String blank  = "                                          ";

字串2

      table[0] = title;

字串7

      table[1] = header;

字串2

      int wk = 2; 字串3

      String line = "";

字串3

      for (int i=1; i

字串6

         line += "     " + ' '; 字串9

      } 字串5

      int days = daysInGregorianMonth(gregorianYear,gregorianMonth);

字串3

      for (int i=gregorianDate; i<=days; i++) {

字串5

         line += getDateString() + ' ';

字串3

         rollUpOneDay(); 字串3

         if (dayOfWeek==1) {

字串8

            table[wk] = line;

字串8

            line = ""; 字串1

            wk++; 字串4

         }

字串7

      }

字串9

      for (int i=dayOfWeek; i<=7; i++) { 字串6

         line += "     " + ' ';

字串6

      } 字串2

      table[wk] = line; 字串8

      for (int i=wk+1; i 字串5

         table[i] = blank;

字串5

      } 字串7

      for (int i=0; i

字串8

         table[i] = table[i].substring(0,table[i].length()-1); 字串2

      }

字串5

 

字串4

      return table;

字串8

   }

字串3

   private static String[] chineseMonthNames =

字串8

      {"","","","","","","","","","","",""};

字串5

   private static String[] principleTermNames = 字串7

      {"雨水","春分","谷雨","夏满","夏至","大暑","处暑","秋分","霜降",

字串5

       "小雪","冬至","大寒"};

字串9

   private static String[] sectionalTermNames =

字串4

      {"立春","惊蛰","清明","立夏","芒种","小暑","立秋","白露","寒露",

字串1

       "立冬","大雪","小寒"}; 字串2

   public String getDateString() {

字串1

      String str = "*  /  "; 字串3

      String gm = String.valueOf(gregorianMonth); 字串6

      if (gm.length()==1) gm = ' ' + gm; 字串6

      String cm = String.valueOf(Math.abs(chineseMonth));

字串7

      if (cm.length()==1) cm = ' ' + cm; 字串5

      String gd = String.valueOf(gregorianDate); 字串2

      if (gd.length()==1) gd = ' ' + gd;

字串4

      String cd = String.valueOf(chineseDate);

字串1

      if (cd.length()==1) cd = ' ' + cd; 字串3

      if (gregorianDate==sectionalTerm) { 字串2

         str = " "+sectionalTermNames[gregorianMonth-1];

字串8

      } else if (gregorianDate==principleTerm) { 字串1

         str = " "+principleTermNames[gregorianMonth-1]; 字串9

      } else if (chineseDate==1 && chineseMonth>0) { 字串1

           str = " "+chineseMonthNames[chineseMonth-1]+""; 字串4

      } else if (chineseDate==1 && chineseMonth<0) {

字串4

           str = "*"+chineseMonthNames[-chineseMonth-1]+""; 字串8

      } else {

字串7

           str = gd+'/'+cd; 字串1

      }

字串7

      return str; 字串6

   } 字串7

   public int rollUpOneDay() { 字串9

      dayOfWeek = dayOfWeek%7 + 1; 字串4

      dayOfYear++;

字串3

      gregorianDate++;

字串9

      int days = daysInGregorianMonth(gregorianYear,gregorianMonth); 字串6

      if (gregorianDate>days) {

字串7

         gregorianDate = 1;

字串5

         gregorianMonth++; 字串8

         if (gregorianMonth>12) { 字串5

            gregorianMonth = 1; 字串4

            gregorianYear++;

字串9

            dayOfYear = 1; 字串6

            isGregorianLeap = isGregorianLeapYear(gregorianYear); 字串2

         }

字串3

         sectionalTerm = sectionalTerm(gregorianYear,gregorianMonth); 字串9

         principleTerm = principleTerm(gregorianYear,gregorianMonth); 字串2

      } 字串7

      chineseDate++;

字串2

      days = daysInChineseMonth(chineseYear,chineseMonth); 字串6

      if (chineseDate>days) {

字串3

         chineseDate = 1;

字串5

         chineseMonth = nextChineseMonth(chineseYear,chineseMonth);

字串6

         if (chineseMonth==1) chineseYear++; 字串9

      }

字串2

      return 0;

字串3

   }

字串1

} 字串2

 
字串7
[/HTML]

字串7

 

字串7

中国二百年年历 1901 年至 2100 字串7

 

字串9

我用上面这个程式制作了二百年年历,1901 年至 2100 年,全部收录在这本书中。 字串3

  字串3

年历格式说明:

字串7

  字串7

农历日期列在公历日期后面。

字串1

节气用节气名称标明。 字串8

农历每月第一天用月份名称标明。

字串7

例如,2000 年一月的表达格式如下: 字串5

[HTML]

字串6

                   一月 字串5

                          

字串8

                                     1/25 字串1

 2/26  3/27  4/28  5/29  立春  腊月  8/ 2

字串2

 9/ 3 10/ 4 11/ 5 12/ 6 13/ 7 14/ 8 15/ 9 字串4

16/10 17/11 18/12 19/13 20/14  雨水 22/16

字串9

23/17 24/18 25/19 26/20 27/21 28/22 29/23

字串1

30/24 31/25 字串6

字串3

字串5
[/HTML]

字串8

  字串1

其中: 字串1

 

字串8

"1/25" - 表示公历 1 号和农历 25 号。 字串5

"立春" - 表示节气。 字串3

"腊月" - 表示农历 12 月第一天。

字串9

作者: 武汉北大青鸟
原载: 武汉北大青鸟鲁广校区 whhpaccp.com
版权所有,转载时必须以链接形式注明作者和原始出处及本声明
高中生入口-武汉北大青鸟
大学生入口-武汉北大青鸟
三校生入口-武汉北大青鸟
去名企入口-武汉北大青鸟

开班时间-武汉北大青鸟

武汉北大青鸟报名咨询
企业人才预定热线
全国免费咨询电话
就业服务电话
教学质量监督
武汉北大青鸟地址
邮编
027-87807737 / 87807787
027-87807736
800-880-0456(电信用户)
027-87807717 李老师
027-87807727 吕老师
武汉洪山区鲁巷广场武汉数码港3楼(鲁巷广场隔壁)
430074  鄂ICP备06014222号