梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
package org.common.website.util;

/**  

 * 
@author wangmj   

 *         编写时间:2005-8-2 <br />  

 *         所在的project名称:00newframe <br />  

 *         类的名称为: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()
    {

    }
    
    
// ====================================== 传回农历 y年的总天数
    
    
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)); // +闰月的天数
        
    }
    
    
// ====================================== 传回农历 y年闰月的天数
    
    
private static int leapDays(int y)
    {
        
        
if (leapMonth(y) != 0)
            
            
return ((lunarInfo[y - 1900& 0x10000== 0 ? 29 : 30);
        
        
else
            
            
return (0);
        
    }
    
    
// ====================================== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
    
    
private static int leapMonth(int y)
    {
        
        
return (lunarInfo[y - 1900& 0xf);
        
    }
    
    
// ====================================== 传回农历 y年m月的总天数
    
    
private static int monthDays(int y, int m)
    {
        
        
return ((lunarInfo[y - 1900& (0x10000 >> m)) == 0 ? 29 : 30);
        
    }
    
    
// ====================================== 算出农历, 传入日期物件, 传回农历日期物件
    
    
// 该物件属性有 .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是农历1900年正月初一
        
        Date baseDate 
= cl.getTime();
        
        
// 1900-01-31是农历1900年正月初一
        
        
int offset = (int) ((objDate.getTime() - baseDate.getTime()) / 86400000); // 天数(86400000=24*60*60*1000)
        
        
// System.out.println(offset);
        
        dayCyl 
= offset + 40// 1899-12-21是农历1899年腊月甲子日
        
        monCyl 
= 14// 1898-10-01是农历甲子月
        
        
// 得到年数
        
        
for (i = 1900; i < 2050 && offset > 0; i++)
        {
            
            temp 
= lYearDays(i); // 农历每年天数
            
            offset 
-= temp;
            
            monCyl 
+= 12;
            
        }
        
        
if (offset < 0)
        {
            
            offset 
+= temp;
            
            i
--;
            
            monCyl 
-= 12;
            
        }
        
        year 
= i; // 农历年份
        
        yearCyl 
= i - 1864// 1864年是甲子年
        
        leap 
= leapMonth(i); // 闰哪个月
        
        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; // 农历月份
        
        day 
= offset + 1// 农历天份
        
        
// 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); 
// 农历
        
        s 
= "农历 " + "" + 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 阅读(1103) 评论(0)  编辑  收藏 所属分类: TOOL

只有注册用户登录后才能发表评论。


网站导航: