夢(mèng)幻之旅

          DEBUG - 天道酬勤

             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評(píng)論 :: 0 Trackbacks
          package org.common.website.util;

          /**  

           * 
          @author wangmj   

           *         編寫(xiě)時(shí)間:2005-8-2 <br />  

           *         所在的project名稱(chēng):00newframe <br />  

           *         類(lèi)的名稱(chēng)為:Lunar.java <br />  

           
          */

          import java.util.Calendar;

          import java.util.Date;

          import org.apache.commons.logging.Log;

          import org.apache.commons.logging.LogFactory;

          // import com.veriweb.util.OurLog;

          public final class Lauar
          {
              
              
          private static Log      log        = LogFactory.getLog(Lauar.class);
              
              
          private static int      monCyl, dayCyl, yearCyl;
              
              
          private static int      year, month, day;
              
              
          private static boolean  isLeap;
              
              
          private static int[]    lunarInfo  = { 0x04bd80x04ae00x0a5700x054d5,

                                                 
          0x0d2600x0d9500x165540x056a0,
                      
          0x09ad00x055d20x04ae0,

                      
          0x0a5b60x0a4d00x0d2500x1d2550x0b5400x0d6a00x0ada2,

                      
          0x095b00x149770x049700x0a4b00x0b4b50x06a500x06d40,

                      
          0x1ab540x02b600x095700x052f20x049700x065660x0d4a0,

                      
          0x0ea500x06e950x05ad00x02b600x186e30x092e00x1c8d7,

                      
          0x0c9500x0d4a00x1d8a60x0b5500x056a00x1a5b40x025d0,

                      
          0x092d00x0d2b20x0a9500x0b5570x06ca00x0b5500x15355,

                      
          0x04da00x0a5d00x145730x052d00x0a9a80x0e9500x06aa0,

                      
          0x0aea60x0ab500x04b600x0aae40x0a5700x052600x0f263,

                      
          0x0d9500x05b570x056a00x096d00x04dd50x04ad00x0a4d0,

                      
          0x0d4d40x0d2500x0d5580x0b5400x0b5a00x195a60x095b0,

                      
          0x049b00x0a9740x0a4b00x0b27a0x06a500x06d400x0af46,

                      
          0x0ab600x095700x04af50x049700x064b00x074a30x0ea50,

                      
          0x06b580x055c00x0ab600x096d50x092e00x0c9600x0d954,

                      
          0x0d4a00x0da500x075520x056a00x0abb70x025d00x092d0,

                      
          0x0cab50x0a9500x0b4a00x0baa40x0ad500x055d90x04ba0,

                      
          0x0a5b00x151760x052b00x0a9300x079540x06aa00x0ad50,

                      
          0x05b520x04b600x0a6e60x0a4e00x0d2600x0ea650x0d530,

                      
          0x05aa00x076a30x096d00x04bd70x04ad00x0a4d00x1d0b6,

                      
          0x0d2500x0d5200x0dd450x0b5a00x056d00x055b20x049b0,

                      
          0x0a5770x0a4b00x0aa500x1b2550x06d200x0ada0 };
              
              
          private static int[]    solarMonth = { 312831303130313130,
                      
          31,

                      
          3031                    };
              
              
          private static String[] Gan        = { """""""""""""",
                      
          "",

                      
          """"                  };
              
              
          private static String[] Zhi        = { """""""""""""",
                      
          "",

                      
          """"""""        };
              
              
          private static String[] Animals    = { """""""""""""",
                      
          "",

                      
          """"""""        };
              
              
          private static int[]    sTermInfo  = { 021208424676383685337,
                      
          107014,

                      
          128867150921173149195551218072240693263343285989,

                      
          308563331033353350375494397447419210440795462224,

                      
          483532504758            };
              
              
          private static String[] nStr1      = { """""""""""""",
                      
          "",

                      
          """"""             };
              
              
          private static String[] nStr2      = { """""廿"""" " };
              
              
          private static String[] monthNong  = { """""""""""""",

                                                 
          """""""""十一""十二" };
              
              
          private static String[] yearName   = { """""""""""""",

                                                 
          """""" };
              
              
          public Lauar()
              {

              }
              
              
          // ====================================== 傳回農(nóng)歷 y年的總天數(shù)
              
              
          private static int lYearDays(int y)
              {
                  
                  
          int i;
                  
                  
          int sum = 348// 29*12
                  
                  
          for (i = 0x8000; i > 0x8; i >>= 1)
                  {
                      
                      
          // OurLog.debug("i="+i);
                      
                      sum 
          += (lunarInfo[y - 1900& i) == 0 ? 0 : 1// 大月+1天
                      
                  }
                  
                  
          return (sum + leapDays(y)); // +閏月的天數(shù)
                  
              }
              
              
          // ====================================== 傳回農(nóng)歷 y年閏月的天數(shù)
              
              
          private static int leapDays(int y)
              {
                  
                  
          if (leapMonth(y) != 0)
                      
                      
          return ((lunarInfo[y - 1900& 0x10000== 0 ? 29 : 30);
                  
                  
          else
                      
                      
          return (0);
                  
              }
              
              
          // ====================================== 傳回農(nóng)歷 y年閏哪個(gè)月 1-12 , 沒(méi)閏傳回 0
              
              
          private static int leapMonth(int y)
              {
                  
                  
          return (lunarInfo[y - 1900& 0xf);
                  
              }
              
              
          // ====================================== 傳回農(nóng)歷 y年m月的總天數(shù)
              
              
          private static int monthDays(int y, int m)
              {
                  
                  
          return ((lunarInfo[y - 1900& (0x10000 >> m)) == 0 ? 29 : 30);
                  
              }
              
              
          // ====================================== 算出農(nóng)歷, 傳入日期物件, 傳回農(nóng)歷日期物件
              
              
          // 該物件屬性有 .year .month .day .isLeap .yearCyl .dayCyl .monCyl
              
              
          private static void Lunar1(Date objDate)
              {
                  
                  
          int i, leap = 0, temp = 0;
                  
                  
          // int monCyl,dayCyl,yearCyl;
                  
                  
          // int year,month,day;
                  
                  
          // boolean isLeap;
                  
                  Calendar cl 
          = Calendar.getInstance();
                  
                  cl.set(
          1900031); // 1900-01-31是農(nóng)歷1900年正月初一
                  
                  Date baseDate 
          = cl.getTime();
                  
                  
          // 1900-01-31是農(nóng)歷1900年正月初一
                  
                  
          int offset = (int) ((objDate.getTime() - baseDate.getTime()) / 86400000); // 天數(shù)(86400000=24*60*60*1000)
                  
                  
          // System.out.println(offset);
                  
                  dayCyl 
          = offset + 40// 1899-12-21是農(nóng)歷1899年臘月甲子日
                  
                  monCyl 
          = 14// 1898-10-01是農(nóng)歷甲子月
                  
                  
          // 得到年數(shù)
                  
                  
          for (i = 1900; i < 2050 && offset > 0; i++)
                  {
                      
                      temp 
          = lYearDays(i); // 農(nóng)歷每年天數(shù)
                      
                      offset 
          -= temp;
                      
                      monCyl 
          += 12;
                      
                  }
                  
                  
          if (offset < 0)
                  {
                      
                      offset 
          += temp;
                      
                      i
          --;
                      
                      monCyl 
          -= 12;
                      
                  }
                  
                  year 
          = i; // 農(nóng)歷年份
                  
                  yearCyl 
          = i - 1864// 1864年是甲子年
                  
                  leap 
          = leapMonth(i); // 閏哪個(gè)月
                  
                  isLeap 
          = false;
                  
                  
          for (i = 1; i < 13 && offset > 0; i++)
                  {
                      
                      
          // 閏月
                      
                      
          if (leap > 0 && i == (leap + 1&& isLeap == false)
                      {
                          
                          
          --i;
                          
                          isLeap 
          = true;
                          
                          temp 
          = leapDays(year);
                          
                      }
                      
          else
                      {
                          
                          temp 
          = monthDays(year, i);
                          
                      }
                      
                      
          // 解除閏月
                      
                      
          if (isLeap == true && i == (leap + 1))
                          
                          isLeap 
          = false;
                      
                      offset 
          -= temp;
                      
                      
          if (isLeap == false)
                          
                          monCyl
          ++;
                      
                  }
                  
                  
          if (offset == 0 && leap > 0 && i == leap + 1)
                      
                      
          if (isLeap)
                      {
                          
                          isLeap 
          = false;
                          
                      }
                      
          else
                      {
                          
                          isLeap 
          = true;
                          
                          
          --i;
                          
                          
          --monCyl;
                          
                      }
                  
                  
          if (offset < 0)
                  {
                      
                      offset 
          += temp;
                      
                      
          --i;
                      
                      
          --monCyl;
                      
                  }
                  
                  month 
          = i; // 農(nóng)歷月份
                  
                  day 
          = offset + 1// 農(nóng)歷天份
                  
                  
          // System.out.println(day);
                  
              }
              
              
          private static int getYear()
              {
                  
                  
          return (year);
                  
              }
              
              
          private static int getMonth()
              {
                  
                  
          return (month);
                  
              }
              
              
          private static int getDay()
              {
                  
                  
          return (day);
                  
              }
              
              
          private static int getMonCyl()
              {
                  
                  
          return (monCyl);
                  
              }
              
              
          private static int getYearCyl()
              {
                  
                  
          return (yearCyl);
                  
              }
              
              
          private static int getDayCyl()
              {
                  
                  
          return (dayCyl);
                  
              }
              
              
          private static boolean getIsLeap()
              {
                  
                  
          return (isLeap);
                  
              }
              
              
          // ============================== 傳入 offset 傳回干支, 0=甲子
              
              
          private static String cyclical(int num)
              {
                  
                  
          return (Gan[num % 10+ Zhi[num % 12]);
                  
              }
              
              
          // ====================== 中文日期
              
              
          private static String cDay(int d)
              {
                  
                  String s;
                  
                  
          switch (d)
                  {
                      
                      
          case 10:

                          s 
          = "初十";
                          
                          
          break;
                      
                      
          case 20:

                          s 
          = "二十";
                          
                          
          break;
                      
                      
          case 30:

                          s 
          = "三十";
                          
                          
          break;
                      
                      
          default:

                          s 
          = nStr2[(int) (d / 10)];// 取商
                          
                          s 
          += nStr1[d % 10];// 取余
                          
                  }
                  
                  
          return (s);
                  
              }
              
              
          private static String cYear(int y)
              {
                  
                  String s 
          = " ";
                  
                  
          int d;
                  
                  
          while (y > 0)
                  {
                      
                      d 
          = y % 10;
                      
                      y 
          = (y - d) / 10;
                      
                      s 
          = yearName[d] + s;
                      
                  }
                  
                  
          return (s);
                  
              }
              
              
          public static String getLunar(String year, String month, String day)
              {
                  
                  Date sDObj;
                  
                  String s;
                  
                  
          int SY, SM, SD;
                  
                  
          int sy;
                  
                  SY 
          = Integer.parseInt(year);
                  
                  SM 
          = Integer.parseInt(month);
                  
                  SD 
          = Integer.parseInt(day);
                  
                  sy 
          = (SY - 4% 12;
                  
                  log.debug(
          "SY=" + SY + "SM=" + SM + "SD=" + SD + "sy=" + sy);
                  
                  Calendar cl 
          = Calendar.getInstance();
                  
                  cl.set(SY, SM 
          - 1, SD);
                  
                  sDObj 
          = cl.getTime();
                  
                  
          // com.veriweb.util.OurLog.debug("sDObj="+sDObj);
                  
                  
          // 日期
                  
                  Lunar1(sDObj); 
          // 農(nóng)歷
                  
                  s 
          = "農(nóng)歷 " + "" + Animals[sy] + "" + cYear(getYear()) + "" + " ";
                  
                  s 
          += (getIsLeap() ? "" : ""+ monthNong[getMonth()] + ""

                  
          + (monthDays(getYear(), getMonth()) == 29 ? "" : "");
                  
                  s 
          += cDay(getDay()) + " ";
                  
                  s 
          += cyclical(getYearCyl()) + "" + cyclical(getMonCyl()) + ""

                  
          + cyclical(getDayCyl()) + "";
                  
                  
          // System.out.println(s);
                  
                  
          return s;
                  
              }
              
              
          public static void main(String[] args)
              {
                  System.out.println(getLunar(
          "2040""7""22"));
              }
              
          }
          posted on 2008-07-28 15:56 HUIKK 閱讀(1122) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): TOOL
          主站蜘蛛池模板: 扶绥县| 肃南| 衡水市| 酒泉市| 米易县| 洪洞县| 青川县| 万荣县| 苍山县| 南充市| 察雅县| 吴堡县| 积石山| 浪卡子县| 沙河市| 辉南县| 龙南县| 晋中市| 莫力| 革吉县| 吉水县| 南宫市| 龙州县| 方正县| 高邑县| 新田县| 隆化县| 邹平县| 丰原市| 长沙市| 东丰县| 厦门市| 察哈| 扎赉特旗| 景洪市| 九寨沟县| 巴林右旗| 建昌县| 昆山市| 冀州市| 屏东县|