分享
 
 
 

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

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

中国公历算法

中国公历算法不是太难,关键是星期值的确定。这里给出了简单算法:

public static int dayOfWeek(int y, int m, int d) {

int w = 1; // 公历一年一月一日是星期一,所以起始值为星期日

y = (y-1)%400 + 1; // 公历星期值分部 400 年循环一次

int ly = (y-1)/4; // 闰年次数

ly = ly - (y-1)/100;

ly = ly + (y-1)/400;

int ry = y - 1 - ly; // 常年次数

w = w + ry; // 常年星期值增一

w = w + 2*ly; // 闰年星期值增二

w = w + dayOfYear(y,m,d);

w = (w-1)%7 + 1;

return w;

}

中国农历算法

根公历相比,中国农历的算法相当复杂。我在网上找的算法之中,eleworld.com 的算法是最好的一个。这个算法使用了大量的数据来确定农历月份和节气的分部,它仅实用于公历 1901 年到 2100 年之间的 200 年。

中国农历计算程式

跟据 eleworld.com 提供的算法,我写了下面这个程式:

[HTML]

/**

* ChineseCalendarGB.java

* Copyright (c) 1997-2002 by Dr. Herong Yang. http://www.herongyang.com/

* 中国农历算法 - 实用于公历 1901 年至 2100 年之间的 200 年

*/

import java.text.*;

import java.util.*;

class ChineseCalendarGB {

private int gregorianYear;

private int gregorianMonth;

private int gregorianDate;

private boolean isGregorianLeap;

private int dayOfYear;

private int dayOfWeek; // 周日一星期的第一天

private int chineseYear;

private int chineseMonth; // 负数表示闰月

private int chineseDate;

private int sectionalTerm;

private int principleTerm;

private static char[] daysInGregorianMonth =

{31,28,31,30,31,30,31,31,30,31,30,31};

private static String[] stemNames =

{"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};

private static String[] branchNames =

{"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};

private static String[] animalNames =

{"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};

public static void main(String[] arg) {

ChineseCalendarGB c = new ChineseCalendarGB();

String cmd = "day";

int y = 1901;

int m = 1;

int d = 1;

if (arg.length>0) cmd = arg[0];

if (arg.length>1) y = Integer.parseInt(arg[1]);

if (arg.length>2) m = Integer.parseInt(arg[2]);

if (arg.length>3) d = Integer.parseInt(arg[3]);

c.setGregorian(y,m,d);

c.computeChineseFields();

c.computeSolarTerms();

if (cmd.equalsIgnoreCase("year")) {

String[] t = c.getYearTable();

for (int i=0; i

} else if (cmd.equalsIgnoreCase("month")) {

String[] t = c.getMonthTable();

for (int i=0; i

} else {

System.out.println(c.toString());

}

}

public ChineseCalendarGB() {

setGregorian(1901,1,1);

}

public void setGregorian(int y, int m, int d) {

gregorianYear = y;

gregorianMonth = m;

gregorianDate = d;

isGregorianLeap = isGregorianLeapYear(y);

dayOfYear = dayOfYear(y,m,d);

dayOfWeek = dayOfWeek(y,m,d);

chineseYear = 0;

chineseMonth = 0;

chineseDate = 0;

sectionalTerm = 0;

principleTerm = 0;

}

public static boolean isGregorianLeapYear(int year) {

boolean isLeap = false;

if (year%4==0) isLeap = true;

if (year%100==0) isLeap = false;

if (year%400==0) isLeap = true;

return isLeap;

}

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

int d = daysInGregorianMonth[m-1];

if (m==2 && isGregorianLeapYear(y)) d++; // 公历闰年二月多一天

return d;

}

public static int dayOfYear(int y, int m, int d) {

int c = 0;

for (int i=1; i

c = c + daysInGregorianMonth(y,i);

}

c = c + d;

return c;

}

public static int dayOfWeek(int y, int m, int d) {

int w = 1; // 公历一年一月一日是星期一,所以起始值为星期日

y = (y-1)%400 + 1; // 公历星期值分部 400 年循环一次

int ly = (y-1)/4; // 闰年次数

ly = ly - (y-1)/100;

ly = ly + (y-1)/400;

int ry = y - 1 - ly; // 常年次数

w = w + ry; // 常年星期值增一

w = w + 2*ly; // 闰年星期值增二

w = w + dayOfYear(y,m,d);

w = (w-1)%7 + 1;

return w;

}

private static char[] chineseMonths = {

// 农历月份大小压缩表,两个字节表示一年。两个字节共十六个二进制位数,

// 前四个位数表示闰月月份,后十二个位数表示十二个农历月份的大小。

0x00,0x04,0xad,0x08,0x5a,0x01,0xd5,0x54,0xb4,0x09,0x64,0x05,0x59,0x45,

0x95,0x0a,0xa6,0x04,0x55,0x24,0xad,0x08,0x5a,0x62,0xda,0x04,0xb4,0x05,

0xb4,0x55,0x52,0x0d,0x94,0x0a,0x4a,0x2a,0x56,0x02,0x6d,0x71,0x6d,0x01,

0xda,0x02,0xd2,0x52,0xa9,0x05,0x49,0x0d,0x2a,0x45,0x2b,0x09,0x56,0x01,

0xb5,0x20,0x6d,0x01,0x59,0x69,0xd4,0x0a,0xa8,0x05,0xa9,0x56,0xa5,0x04,

0x2b,0x09,0x9e,0x38,0xb6,0x08,0xec,0x74,0x6c,0x05,0xd4,0x0a,0xe4,0x6a,

0x52,0x05,0x95,0x0a,0x5a,0x42,0x5b,0x04,0xb6,0x04,0xb4,0x22,0x6a,0x05,

0x52,0x75,0xc9,0x0a,0x52,0x05,0x35,0x55,0x4d,0x0a,0x5a,0x02,0x5d,0x31,

0xb5,0x02,0x6a,0x8a,0x68,0x05,0xa9,0x0a,0x8a,0x6a,0x2a,0x05,0x2d,0x09,

0xaa,0x48,0x5a,0x01,0xb5,0x09,0xb0,0x39,0x64,0x05,0x25,0x75,0x95,0x0a,

0x96,0x04,0x4d,0x54,0xad,0x04,0xda,0x04,0xd4,0x44,0xb4,0x05,0x54,0x85,

0x52,0x0d,0x92,0x0a,0x56,0x6a,0x56,0x02,0x6d,0x02,0x6a,0x41,0xda,0x02,

0xb2,0xa1,0xa9,0x05,0x49,0x0d,0x0a,0x6d,0x2a,0x09,0x56,0x01,0xad,0x50,

0x6d,0x01,0xd9,0x02,0xd1,0x3a,0xa8,0x05,0x29,0x85,0xa5,0x0c,0x2a,0x09,

0x96,0x54,0xb6,0x08,0x6c,0x09,0x64,0x45,0xd4,0x0a,0xa4,0x05,0x51,0x25,

0x95,0x0a,0x2a,0x72,0x5b,0x04,0xb6,0x04,0xac,0x52,0x6a,0x05,0xd2,0x0a,

0xa2,0x4a,0x4a,0x05,0x55,0x94,0x2d,0x0a,0x5a,0x02,0x75,0x61,0xb5,0x02,

0x6a,0x03,0x61,0x45,0xa9,0x0a,0x4a,0x05,0x25,0x25,0x2d,0x09,0x9a,0x68,

0xda,0x08,0xb4,0x09,0xa8,0x59,0x54,0x03,0xa5,0x0a,0x91,0x3a,0x96,0x04,

0xad,0xb0,0xad,0x04,0xda,0x04,0xf4,0x62,0xb4,0x05,0x54,0x0b,0x44,0x5d,

0x52,0x0a,0x95,0x04,0x55,0x22,0x6d,0x02,0x5a,0x71,0xda,0x02,0xaa,0x05,

0xb2,0x55,0x49,0x0b,0x4a,0x0a,0x2d,0x39,0x36,0x01,0x6d,0x80,0x6d,0x01,

0xd9,0x02,0xe9,0x6a,0xa8,0x05,0x29,0x0b,0x9a,0x4c,0xaa,0x08,0xb6,0x08,

0xb4,0x38,0x6c,0x09,0x54,0x75,0xd4,0x0a,0xa4,0x05,0x45,0x55,0x95,0x0a,

0x9a,0x04,0x55,0x44,0xb5,0x04,0x6a,0x82,0x6a,0x05,0xd2,0x0a,0x92,0x6a,

0x4a,0x05,0x55,0x0a,0x2a,0x4a,0x5a,0x02,0xb5,0x02,0xb2,0x31,0x69,0x03,

0x31,0x73,0xa9,0x0a,0x4a,0x05,0x2d,0x55,0x2d,0x09,0x5a,0x01,0xd5,0x48,

0xb4,0x09,0x68,0x89,0x54,0x0b,0xa4,0x0a,0xa5,0x6a,0x95,0x04,0xad,0x08,

0x6a,0x44,0xda,0x04,0x74,0x05,0xb0,0x25,0x54,0x03

};

// 初始日,公历农历对应日期:

// 公历 1901 年 1 月 1 日,对应农历 4598 年 11 月 11 日

private static int baseYear = 1901;

private static int baseMonth = 1;

private static int baseDate = 1;

private static int baseIndex = 0;

private static int baseChineseYear = 4598-1;

private static int baseChineseMonth = 11;

private static int baseChineseDate = 11;

public int computeChineseFields() {

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

int startYear = baseYear;

int startMonth = baseMonth;

int startDate = baseDate;

chineseYear = baseChineseYear;

chineseMonth = baseChineseMonth;

chineseDate = baseChineseDate;

// 第二个对应日,用以提高计算效率

// 公历 2000 年 1 月 1 日,对应农历 4697 年 11 月 25 日

if (gregorianYear >= 2000) {

startYear = baseYear + 99;

startMonth = 1;

startDate = 1;

chineseYear = baseChineseYear + 99;

chineseMonth = 11;

chineseDate = 25;

}

int daysDiff = 0;

for (int i=startYear; i

daysDiff += 365;

if (isGregorianLeapYear(i)) daysDiff += 1; // leap year

}

for (int i=startMonth; i

daysDiff += daysInGregorianMonth(gregorianYear,i);

}

daysDiff += gregorianDate - startDate;

chineseDate += daysDiff;

int lastDate = daysInChineseMonth(chineseYear, chineseMonth);

int nextMonth = nextChineseMonth(chineseYear, chineseMonth);

while (chineseDate>lastDate) {

if (Math.abs(nextMonth)

chineseMonth = nextMonth;

chineseDate -= lastDate;

lastDate = daysInChineseMonth(chineseYear, chineseMonth);

nextMonth = nextChineseMonth(chineseYear, chineseMonth);

}

return 0;

}

private static int[] bigLeapMonthYears = {

// 大闰月的闰年年份

6, 14, 19, 25, 33, 36, 38, 41, 44, 52,

55, 79,117,136,147,150,155,158,185,193

};

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有