java阳历转换成阴历

来源:互联网 发布:大数据对电商的影响 编辑:程序博客网 时间:2024/04/30 08:06
  1. import java.util.Calendar;  
  2.   
  3. import java.util.Date;  
  4.   
  5.   
  6.   
  7. import org.apache.commons.logging.Log;  
  8.   
  9. import org.apache.commons.logging.LogFactory;  
  10.   
  11.   
  12.   
  13. //import com.veriweb.util.OurLog;  
  14.   
  15. public final class Lauar {  
  16.   
  17.     private static Log log = LogFactory.getLog(Lauar.class);  
  18.   
  19.   
  20.   
  21.     private static int monCyl, dayCyl, yearCyl;  
  22.   
  23.   
  24.   
  25.     private static int year, month, day;  
  26.   
  27.   
  28.   
  29.     private static boolean isLeap;  
  30.   
  31.   
  32.   
  33.     private static int[] lunarInfo = { 0x04bd80x04ae00x0a5700x054d5,  
  34.   
  35.             0x0d2600x0d9500x165540x056a00x09ad00x055d20x04ae0,  
  36.   
  37.             0x0a5b60x0a4d00x0d2500x1d2550x0b5400x0d6a00x0ada2,  
  38.   
  39.             0x095b00x149770x049700x0a4b00x0b4b50x06a500x06d40,  
  40.   
  41.             0x1ab540x02b600x095700x052f20x049700x065660x0d4a0,  
  42.   
  43.             0x0ea500x06e950x05ad00x02b600x186e30x092e00x1c8d7,  
  44.   
  45.             0x0c9500x0d4a00x1d8a60x0b5500x056a00x1a5b40x025d0,  
  46.   
  47.             0x092d00x0d2b20x0a9500x0b5570x06ca00x0b5500x15355,  
  48.   
  49.             0x04da00x0a5d00x145730x052d00x0a9a80x0e9500x06aa0,  
  50.   
  51.             0x0aea60x0ab500x04b600x0aae40x0a5700x052600x0f263,  
  52.   
  53.             0x0d9500x05b570x056a00x096d00x04dd50x04ad00x0a4d0,  
  54.   
  55.             0x0d4d40x0d2500x0d5580x0b5400x0b5a00x195a60x095b0,  
  56.   
  57.             0x049b00x0a9740x0a4b00x0b27a0x06a500x06d400x0af46,  
  58.   
  59.             0x0ab600x095700x04af50x049700x064b00x074a30x0ea50,  
  60.   
  61.             0x06b580x055c00x0ab600x096d50x092e00x0c9600x0d954,  
  62.   
  63.             0x0d4a00x0da500x075520x056a00x0abb70x025d00x092d0,  
  64.   
  65.             0x0cab50x0a9500x0b4a00x0baa40x0ad500x055d90x04ba0,  
  66.   
  67.             0x0a5b00x151760x052b00x0a9300x079540x06aa00x0ad50,  
  68.   
  69.             0x05b520x04b600x0a6e60x0a4e00x0d2600x0ea650x0d530,  
  70.   
  71.             0x05aa00x076a30x096d00x04bd70x04ad00x0a4d00x1d0b6,  
  72.   
  73.             0x0d2500x0d5200x0dd450x0b5a00x056d00x055b20x049b0,  
  74.   
  75.             0x0a5770x0a4b00x0aa500x1b2550x06d200x0ada0 };  
  76.   
  77.   
  78.   
  79.     private static int[] solarMonth = { 31283130313031313031,  
  80.   
  81.             3031 };  
  82.   
  83.   
  84.   
  85.     private static String[] Gan = { "甲""乙""丙""丁""戊""己""庚""辛",  
  86.   
  87.             "壬""癸" };  
  88.   
  89.   
  90.   
  91.     private static String[] Zhi = { "子""丑""寅""卯""辰""巳""午""未",  
  92.   
  93.             "申""酉""戌""亥" };  
  94.   
  95.   
  96.   
  97.     private static String[] Animals = { "鼠""牛""虎""兔""龙""蛇""马""羊",  
  98.   
  99.             "猴""鸡""狗""猪" };  
  100.   
  101.   
  102.   
  103.     private static int[] sTermInfo = { 021208424676383685337107014,  
  104.   
  105.             128867150921173149195551218072240693263343285989,  
  106.   
  107.             308563331033353350375494397447419210440795462224,  
  108.   
  109.             483532504758 };  
  110.   
  111.   
  112.   
  113.     private static String[] nStr1 = { "日""一""二""三""四""五""六""七",  
  114.   
  115.             "八""九""十" };  
  116.   
  117.   
  118.   
  119.     private static String[] nStr2 = { "初""十""廿""卅"" " };  
  120.   
  121.   
  122.   
  123.     private static String[] monthNong = { "正""正""二""三""四""五""六",  
  124.   
  125.             "七""八""九""十""十一""十二" };  
  126.   
  127.   
  128.   
  129.     private static String[] yearName = { "零""壹""贰""叁""肆""伍""陆",  
  130.   
  131.             "柒""捌""玖" };  
  132.   
  133.   
  134.   
  135.     public Lauar() {  
  136.   
  137.     }  
  138.   
  139.   
  140.   
  141.     //====================================== 传回农历 y年的总天数  
  142.   
  143.     private static int lYearDays(int y) {  
  144.   
  145.         int i;  
  146.   
  147.         int sum = 348//29*12  
  148.   
  149.         for (i = 0x8000; i > 0x8; i >>= 1) {  
  150.   
  151.             //OurLog.debug("i="+i);  
  152.   
  153.             sum += (lunarInfo[y - 1900] & i) == 0 ? 0 : 1//大月+1天  
  154.   
  155.         }  
  156.   
  157.         return (sum + leapDays(y)); //+闰月的天数  
  158.   
  159.     }  
  160.   
  161.   
  162.   
  163.     //====================================== 传回农历 y年闰月的天数  
  164.   
  165.     private static int leapDays(int y) {  
  166.   
  167.         if (leapMonth(y) != 0)  
  168.   
  169.             return ((lunarInfo[y - 1900] & 0x10000) == 0 ? 29 : 30);  
  170.   
  171.         else  
  172.   
  173.             return (0);  
  174.   
  175.     }  
  176.   
  177.   
  178.   
  179.     //====================================== 传回农历 y年闰哪个月 1-12 , 没闰传回 0  
  180.   
  181.     private static int leapMonth(int y) {  
  182.   
  183.         return (lunarInfo[y - 1900] & 0xf);  
  184.   
  185.     }  
  186.   
  187.   
  188.   
  189.     //====================================== 传回农历 y年m月的总天数  
  190.   
  191.     private static int monthDays(int y, int m) {  
  192.   
  193.         return ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0 ? 29 : 30);  
  194.   
  195.     }  
  196.   
  197.   
  198.   
  199.     //====================================== 算出农历, 传入日期物件, 传回农历日期物件  
  200.   
  201.     //                                      该物件属性有 .year .month .day .isLeap .yearCyl .dayCyl .monCyl  
  202.   
  203.     private static void Lunar1(Date objDate) {  
  204.   
  205.         int i, leap = 0, temp = 0;  
  206.   
  207.         //int monCyl,dayCyl,yearCyl;  
  208.   
  209.         //int year,month,day;  
  210.   
  211.         //boolean isLeap;  
  212.   
  213.         Calendar cl = Calendar.getInstance();  
  214.   
  215.         cl.set(1900031); //1900-01-31是农历1900年正月初一  
  216.   
  217.         Date baseDate = cl.getTime();  
  218.   
  219.         //1900-01-31是农历1900年正月初一  
  220.   
  221.         int offset = (int) ((objDate.getTime() - baseDate.getTime()) / 86400000); //天数(86400000=24*60*60*1000)  
  222.   
  223.         //System.out.println(offset);  
  224.   
  225.         dayCyl = offset + 40//1899-12-21是农历1899年腊月甲子日  
  226.   
  227.         monCyl = 14//1898-10-01是农历甲子月  
  228.   
  229.         //得到年数  
  230.   
  231.         for (i = 1900; i < 2050 && offset > 0; i++) {  
  232.   
  233.             temp = lYearDays(i); //农历每年天数  
  234.   
  235.             offset -= temp;  
  236.   
  237.             monCyl += 12;  
  238.   
  239.         }  
  240.   
  241.         if (offset < 0) {  
  242.   
  243.             offset += temp;  
  244.   
  245.             i--;  
  246.   
  247.             monCyl -= 12;  
  248.   
  249.         }  
  250.   
  251.         year = i; //农历年份  
  252.   
  253.         yearCyl = i - 1864//1864年是甲子年  
  254.   
  255.         leap = leapMonth(i); //闰哪个月  
  256.   
  257.         isLeap = false;  
  258.   
  259.         for (i = 1; i < 13 && offset > 0; i++) {  
  260.   
  261.             //闰月  
  262.   
  263.             if (leap > 0 && i == (leap + 1) && isLeap == false) {  
  264.   
  265.                 --i;  
  266.   
  267.                 isLeap = true;  
  268.   
  269.                 temp = leapDays(year);  
  270.   
  271.             } else {  
  272.   
  273.                 temp = monthDays(year, i);  
  274.   
  275.             }  
  276.   
  277.             //解除闰月  
  278.   
  279.             if (isLeap == true && i == (leap + 1))  
  280.   
  281.                 isLeap = false;  
  282.   
  283.             offset -= temp;  
  284.   
  285.             if (isLeap == false)  
  286.   
  287.                 monCyl++;  
  288.   
  289.         }  
  290.   
  291.         if (offset == 0 && leap > 0 && i == leap + 1)  
  292.   
  293.             if (isLeap) {  
  294.   
  295.                 isLeap = false;  
  296.   
  297.             } else {  
  298.   
  299.                 isLeap = true;  
  300.   
  301.                 --i;  
  302.   
  303.                 --monCyl;  
  304.   
  305.             }  
  306.   
  307.         if (offset < 0) {  
  308.   
  309.             offset += temp;  
  310.   
  311.             --i;  
  312.   
  313.             --monCyl;  
  314.   
  315.         }  
  316.   
  317.         month = i; //农历月份  
  318.   
  319.         day = offset + 1//农历天份  
  320.   
  321.         //System.out.println(day);  
  322.   
  323.     }  
  324.   
  325.   
  326.   
  327.     private static int getYear() {  
  328.   
  329.         return (year);  
  330.   
  331.     }  
  332.   
  333.   
  334.   
  335.     private static int getMonth() {  
  336.   
  337.         return (month);  
  338.   
  339.     }  
  340.   
  341.   
  342.   
  343.     private static int getDay() {  
  344.   
  345.         return (day);  
  346.   
  347.     }  
  348.   
  349.   
  350.   
  351.     private static int getMonCyl() {  
  352.   
  353.         return (monCyl);  
  354.   
  355.     }  
  356.   
  357.   
  358.   
  359.     private static int getYearCyl() {  
  360.   
  361.         return (yearCyl);  
  362.   
  363.     }  
  364.   
  365.   
  366.   
  367.     private static int getDayCyl() {  
  368.   
  369.         return (dayCyl);  
  370.   
  371.     }  
  372.   
  373.   
  374.   
  375.     private static boolean getIsLeap() {  
  376.   
  377.         return (isLeap);  
  378.   
  379.     }  
  380.   
  381.   
  382.   
  383.     //============================== 传入 offset 传回干支, 0=甲子  
  384.   
  385.     private static String cyclical(int num) {  
  386.   
  387.         return (Gan[num % 10] + Zhi[num % 12]);  
  388.   
  389.     }  
  390.   
  391.   
  392.   
  393.     //====================== 中文日期  
  394.   
  395.     private static String cDay(int d) {  
  396.   
  397.         String s;  
  398.   
  399.         switch (d) {  
  400.   
  401.         case 10:  
  402.   
  403.             s = "初十";  
  404.   
  405.             break;  
  406.   
  407.         case 20:  
  408.   
  409.             s = "二十";  
  410.   
  411.             break;  
  412.   
  413.         case 30:  
  414.   
  415.             s = "三十";  
  416.   
  417.             break;  
  418.   
  419.         default:  
  420.   
  421.             s = nStr2[(int) (d / 10)];//取商  
  422.   
  423.             s += nStr1[d % 10];//取余  
  424.   
  425.         }  
  426.   
  427.         return (s);  
  428.   
  429.     }  
  430.   
  431.   
  432.   
  433.     private static String cYear(int y) {  
  434.   
  435.         String s = " ";  
  436.   
  437.         int d;  
  438.   
  439.         while (y > 0) {  
  440.   
  441.             d = y % 10;  
  442.   
  443.             y = (y - d) / 10;  
  444.   
  445.             s = yearName[d] + s;  
  446.   
  447.         }  
  448.   
  449.         return (s);  
  450.   
  451.     }  
  452.   
  453.   
  454.   
  455.     public static String getLunar(String year, String month, String day) {  
  456.   
  457.   
  458.   
  459.         Date sDObj;  
  460.   
  461.         String s;  
  462.   
  463.         int SY, SM, SD;  
  464.   
  465.   
  466.   
  467.         int sy;  
  468.   
  469.         SY = Integer.parseInt(year);  
  470.   
  471.         SM = Integer.parseInt(month);  
  472.   
  473.         SD = Integer.parseInt(day);  
  474.   
  475.         sy = (SY - 4) % 12;  
  476.   
  477.         log.debug("SY=" + SY + "SM=" + SM + "SD=" + SD + "sy=" + sy);  
  478.   
  479.         Calendar cl = Calendar.getInstance();  
  480.   
  481.         cl.set(SY, SM - 1, SD);  
  482.   
  483.         sDObj = cl.getTime();  
  484.   
  485.         //com.veriweb.util.OurLog.debug("sDObj="+sDObj);  
  486.   
  487.   
  488.   
  489.         //日期  
  490.   
  491.         Lunar1(sDObj); //农历  
  492.   
  493.         s = "农历 " + "【" + Animals[sy] + "】" + cYear(getYear()) + "年" + " ";  
  494.   
  495.   
  496.   
  497.         s += (getIsLeap() ? "闰" : "") + monthNong[getMonth()] + "月"  
  498.   
  499.                 + (monthDays(getYear(), getMonth()) == 29 ? "小" : "大");  
  500.   
  501.         s += cDay(getDay()) + " ";  
  502.   
  503.         s += cyclical(getYearCyl()) + "年" + cyclical(getMonCyl()) + "月"  
  504.   
  505.                 + cyclical(getDayCyl()) + "日";  
  506.   
  507.         // System.out.println(s);  
  508.   
  509.         return s;  
  510.   
  511.   
  512.   
  513.     }  
  514.   
  515.   
  516.   
  517.     public static void main(String[] args) {  
  518.   
  519.         System.out.println(getLunar("2005""8""2"));  
  520.   
  521.         System.out.println(getLunar("2006""8""3"));  
  522.   
  523.         System.out.println(getLunar("2008""6""8"));  
  524.   
  525.         System.out.println(getLunar("2010""8""3"));  
  526.   
  527.         System.out.println(getLunar("2010""7""18"));  
  528.   
  529.         //        农历 【鸡】贰零零伍 年 六月大廿八 乙酉年癸未月戊午日  
  530.   
  531.         //        农历 【狗】贰零零陆 年 七月大初十 丙戌年丙申月甲子日  
  532.   
  533.         //        农历 【虎】贰零壹零 年 六月小廿三 庚寅年癸未月乙酉日  
  534.   
  535.         //=============修改之前VS修改之后============  
  536.   
  537.         //        农历 【鸡】贰零零伍 年 六月大廿八 乙酉年癸未月戊午日  
  538.   
  539.         //        农历 【狗】贰零零陆 年 七月大初十 丙戌年丙申月甲子日  
  540.   
  541.         //        农历 【虎】贰零壹零 年 六月小廿三 庚寅年癸未月乙酉日  
  542.   
  543.     }  
  544.   
  545. }