服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

      // 注意:闰月 m < 0

      int index = y - basechineseyear + baseindex;

      int v = 0;

      int l = 0;

      int d = 30;

      if (1<=m && m<=8) {

         v = chinesemonths[2*index];

         l = m - 1;

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

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

         v = chinesemonths[2*index+1];

         l = m - 9;

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

      } else {

         v = chinesemonths[2*index+1];

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

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

            d = 0;

         } else {

            d = 29;

            for (int i=0; i

               if (bigleapmonthyears[i]==index) {

                  d = 30;

                  break;

               }

            }

         }

      }

      return d;

   }

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

      int n = math.abs(m) + 1;

      if (m>0) {

         int index = y - basechineseyear + baseindex;

         int v = chinesemonths[2*index+1];

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

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

      }

      if (n==13) n = 1;

      return n;

   }

   private static char[][] sectionaltermmap = {

   {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},

   {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},

   {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},

   {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,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},

   {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},

   {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},

   {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},

   {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},

   {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},

   {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,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}

   };

   private static char[][] sectionaltermyear = {

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

   {13,45,81,117,149,185,201,250,250},

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

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

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

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

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

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

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

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

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

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

   };

   private static char[][] principletermmap = {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

   };

   private static char[][] principletermyear = {

   {13,45,81,113,149,185,201},

   {21,57,93,125,161,193,201},

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

   {21,49,81,116,144,176,200,201},

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

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

   {25,53,84,112,144,172,200,201},

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

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

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

   {16,44,80,112,148,180,200,201},

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

   };

   public int computesolarterms() {

      if (gregorianyear<1901 || gregorianyear>2100) return 1;

      sectionalterm = sectionalterm(gregorianyear, gregorianmonth);

      principleterm = principleterm(gregorianyear, gregorianmonth);

      return 0;

   }

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

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

      int index = 0;

      int ry = y-baseyear+1;

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

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

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

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

      if ((ry == 194)&&(m == 6)) term = 6;

      return term;

   }

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

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

      int index = 0;

      int ry = y-baseyear+1;

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

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

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

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

      return term;

   }

   public string tostring() {

      stringbuffer buf = new stringbuffer();

      buf.append("gregorian year: "+gregorianyear+"/n");

      buf.append("gregorian month: "+gregorianmonth+"/n");

      buf.append("gregorian date: "+gregoriandate+"/n");

      buf.append("is leap year: "+isgregorianleap+"/n");

      buf.append("day of year: "+dayofyear+"/n");

      buf.append("day of week: "+dayofweek+"/n");

      buf.append("chinese year: "+chineseyear+"/n");

      buf.append("heavenly stem: "+((chineseyear-1)%10)+"/n");

      buf.append("earthly branch: "+((chineseyear-1)%12)+"/n");

      buf.append("chinese month: "+chinesemonth+"/n");

      buf.append("chinese date: "+chinesedate+"/n");

      buf.append("sectional term: "+sectionalterm+"/n");

      buf.append("principle term: "+principleterm+"/n");

      return buf.tostring();

   }

   public string[] getyeartable() {

      setgregorian(gregorianyear,1,1);

      computechinesefields();

      computesolarterms();

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

      table[0] = gettextline(27, "公历年历:"+gregorianyear);

      table[1] = gettextline(27, "农历年历:"+(chineseyear+1)

         + " ("+stemnames[(chineseyear+1-1)%10]

         + branchnames[(chineseyear+1-1)%12]

         + " - "+animalnames[(chineseyear+1-1)%12]+")");

      int ln = 2;

      string blank  = "                                         "

              +"  " + "                                         ";

      string[] mleft = null;

      string[] mright = null;

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

         table[ln] = blank;

         ln++;

         mleft = getmonthtable();

         mright = getmonthtable();

         for (int j=0; j

            string line = mleft[j] + "  " + mright[j];

            table[ln] = line;

            ln++;

         }

      }

      table[ln] = blank;

      ln++;

      table[ln] = gettextline(0,

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

      ln++;

      return table;

   }

   public static string gettextline(int s, string t) {

      string str  = "                                         "

              +"  " + "                                         ";

      if (t!=null && s

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

      return str;

   }

   private static string[] monthnames =

      {"","","","","","","","","","","十一","十二"};

   public string[] getmonthtable() {

      setgregorian(gregorianyear,gregorianmonth,1);

      computechinesefields();

      computesolarterms();

      string[] table = new string[8];

      string title  = null;

      if (gregorianmonth<11) title  = "                   ";

      else title  = "                 ";

      title = title + monthnames[gregorianmonth-1] + ""

                    + "                   ";

      string header = "                           ";

      string blank  = "                                          ";

      table[0] = title;

      table[1] = header;

      int wk = 2;

      string line = "";

      for (int i=1; i

         line += "     " + ' ';

      }

      int days = daysingregorianmonth(gregorianyear,gregorianmonth);

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

         line += getdatestring() + ' ';

         rolluponeday();

         if (dayofweek==1) {

            table[wk] = line;

            line = "";

            wk++;

         }

      }

      for (int i=dayofweek; i<=7; i++) {

         line += "     " + ' ';

      }

      table[wk] = line;

      for (int i=wk+1; i

         table[i] = blank;

      }

      for (int i=0; i

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

      }

 

      return table;

   }

   private static string[] chinesemonthnames =