对前几天作的java万年历,再弄修改,加上农历

本文介绍了一种将阳历转换为农历的算法,并提供了详细的Java实现代码。该算法能够计算出农历的具体日期、生肖及干支等信息,适用于需要进行日期转换的应用场景。
     前几天在blog中,对网友的java 万年历作修改,看到有的网友说能不能加上农历,后来在网上看到有人写过几个阳历转阴历的算法,我比较了一个发现,这个算法还算不错,只要有的计算机编程基础的人看明白应该是没有问题的,其实这个就和我们以前在c中,判断一天是周几的算法差不多,都是和某一个特定的时间作比较,算出差多少天,再根据月大月小瑞月这些规则,算出是农历的那年那月那日.
None.gif package  clock;
None.gif
None.gif
import  java.text.ParseException;
None.gif
import  java.text.SimpleDateFormat;
None.gif
import  java.util.Calendar;
None.gif
import  java.util.Date;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Lunar  dot.gif {
InBlock.gif    
private int year;
InBlock.gif    
private int month;
InBlock.gif    
private int day;
InBlock.gif    
private boolean leap;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
final static String chineseNumber[] = dot.gif{"""""""""""""""""""""十一""十二"};
InBlock.gif    
static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
InBlock.gif    
final static long[] lunarInfo = new long[]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{0x04bd80x04ae00x0a5700x054d50x0d2600x0d9500x165540x056a00x09ad00x055d2,
InBlock.gif     
0x04ae00x0a5b60x0a4d00x0d2500x1d2550x0b5400x0d6a00x0ada20x095b00x14977,
InBlock.gif     
0x049700x0a4b00x0b4b50x06a500x06d400x1ab540x02b600x095700x052f20x04970,
InBlock.gif     
0x065660x0d4a00x0ea500x06e950x05ad00x02b600x186e30x092e00x1c8d70x0c950,
InBlock.gif     
0x0d4a00x1d8a60x0b5500x056a00x1a5b40x025d00x092d00x0d2b20x0a9500x0b557,
InBlock.gif     
0x06ca00x0b5500x153550x04da00x0a5d00x145730x052d00x0a9a80x0e9500x06aa0,
InBlock.gif     
0x0aea60x0ab500x04b600x0aae40x0a5700x052600x0f2630x0d9500x05b570x056a0,
InBlock.gif     
0x096d00x04dd50x04ad00x0a4d00x0d4d40x0d2500x0d5580x0b5400x0b5a00x195a6,
InBlock.gif     
0x095b00x049b00x0a9740x0a4b00x0b27a0x06a500x06d400x0af460x0ab600x09570,
InBlock.gif     
0x04af50x049700x064b00x074a30x0ea500x06b580x055c00x0ab600x096d50x092e0,
InBlock.gif     
0x0c9600x0d9540x0d4a00x0da500x075520x056a00x0abb70x025d00x092d00x0cab5,
InBlock.gif     
0x0a9500x0b4a00x0baa40x0ad500x055d90x04ba00x0a5b00x151760x052b00x0a930,
InBlock.gif     
0x079540x06aa00x0ad500x05b520x04b600x0a6e60x0a4e00x0d2600x0ea650x0d530,
InBlock.gif     
0x05aa00x076a30x096d00x04bd70x04ad00x0a4d00x1d0b60x0d2500x0d5200x0dd45,
ExpandedSubBlockEnd.gif     
0x0b5a00x056d00x055b20x049b00x0a5770x0a4b00x0aa500x1b2550x06d200x0ada0}
;
InBlock.gif
InBlock.gif    
//====== 传回农历 y年的总天数
ExpandedSubBlockStart.gifContractedSubBlock.gif
    final private static int yearDays(int y) dot.gif{
InBlock.gif        
int i, sum = 348;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for (i = 0x8000; i > 0x8; i >>= 1dot.gif{
InBlock.gif            
if ((lunarInfo[y - 1900& i) != 0) sum += 1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return (sum + leapDays(y));
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//====== 传回农历 y年闰月的天数
ExpandedSubBlockStart.gifContractedSubBlock.gif
    final private static int leapDays(int y) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (leapMonth(y) != 0dot.gif{
InBlock.gif            
if ((lunarInfo[y - 1900& 0x10000!= 0)
InBlock.gif                
return 30;
InBlock.gif            
else
InBlock.gif                
return 29;
ExpandedSubBlockEnd.gif        }
 else
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
ExpandedSubBlockStart.gifContractedSubBlock.gif
    final private static int leapMonth(int y) dot.gif{
InBlock.gif        
return (int) (lunarInfo[y - 1900& 0xf);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//====== 传回农历 y年m月的总天数
ExpandedSubBlockStart.gifContractedSubBlock.gif
    final private static int monthDays(int y, int m) dot.gif{
InBlock.gif        
if ((lunarInfo[y - 1900& (0x10000 >> m)) == 0)
InBlock.gif            
return 29;
InBlock.gif        
else
InBlock.gif            
return 30;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//====== 传回农历 y年的生肖
ExpandedSubBlockStart.gifContractedSubBlock.gif
    final public String animalsYear() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
final String[] Animals = new String[]dot.gif{""""""""""""""""""""""""};
InBlock.gif        
return Animals[(year - 4% 12];
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//====== 传入 月日的offset 传回干支, 0=甲子
ExpandedSubBlockStart.gifContractedSubBlock.gif
    final private static String cyclicalm(int num) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
final String[] Gan = new String[]dot.gif{""""""""""""""""""""};
ExpandedSubBlockStart.gifContractedSubBlock.gif        
final String[] Zhi = new String[]dot.gif{""""""""""""""""""""""""};
InBlock.gif        
return (Gan[num % 10+ Zhi[num % 12]);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//====== 传入 offset 传回干支, 0=甲子
ExpandedSubBlockStart.gifContractedSubBlock.gif
    final public String cyclical() dot.gif{
InBlock.gif        
int num = year - 1900 + 36;
InBlock.gif        
return (cyclicalm(num));
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 传出y年m月d日对应的农历.
InBlock.gif     * yearCyl3:农历年与1864的相差数              ?
InBlock.gif     * monCyl4:从1900年1月31日以来,闰月数
InBlock.gif     * dayCyl5:与1900年1月31日相差的天数,再加40      ?
InBlock.gif     * 
@param cal 
InBlock.gif     * 
@return 
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Lunar(Calendar cal) dot.gif{
InBlock.gif        @SuppressWarnings(
"unused"int yearCyl, monCyl, dayCyl;
InBlock.gif        
int leapMonth = 0;
InBlock.gif        Date baseDate 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            baseDate 
= chineseDateFormat.parse("1900年1月31日");
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (ParseException e) dot.gif{
InBlock.gif            e.printStackTrace();  
//To change body of catch statement use Options | File Templates.
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
//求出和1900年1月31日相差的天数
InBlock.gif
        int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
InBlock.gif        dayCyl 
= offset + 40;
InBlock.gif        monCyl 
= 14;
InBlock.gif
InBlock.gif        
//用offset减去每农历年的天数
InBlock.gif        
// 计算当天是农历第几天
InBlock.gif        
//i最终结果是农历的年份
InBlock.gif        
//offset是当年的第几天
InBlock.gif
        in
LunarCalendar返回农历(阴历)日期的JAR包 根据指定日期计算对应农历日期(这个计算方法是网上找的,最初的者是谁已经无法考证了,感谢网络资源吧!),本人封装成好用的JAR包后发不出来,供大家免费下载! toString()方法输出阴历日期(例如:癸巳年七月廿) getFullInfo()方法输出包括生肖在内的阴历日期(例如:癸巳年七月廿,生肖:蛇) 构建方法包括以下四种: public LunarCalendar(String year, String month, String date) public LunarCalendar(JComboBox jcYear, JComboBox jcMonth, JComboBox jcDate) public LunarCalendar(int year, int month, int date) public LunarCalendar(Calendar cal)) 使用两种构建方法时,若文本内容不为数字,getErrorMessage会返回错误信息 方法摘要 java.lang.String getErrorMessage() 返回String类型的错误信息 java.lang.String getFullInfo() 返回String类型的详细阴历信息(例如:癸巳年七月廿,生肖:蛇) java.lang.String getLunarAnimal() 返回String类型的生肖(例如:蛇) java.lang.String getLunarDate() 返回String类型的阴历日期(例如:廿) java.lang.String getLunarMonth() 返回String类型的阴历月份(例如:七) java.lang.String getLunarYear() 返回String类型的阴历年份(天干地支,例如:癸巳) java.lang.String toString() 返回String类型的阴历日期(例如:癸巳年七月廿) JAR包名称:LunarCalendar version 1.0 8/26/2013 者:Roy, Liu royliu90@live.cn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值