2024年3月14日发(作者:)
中国农历二百年算法及年历-中国年历算法和程式
中国公历算法
中国公历算法不是太难,关键是星期值的确定。这里给出了简单
算法:
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;
}
中国农历算法
根公历相比,中国农历的算法相当复杂。我在网上找的算法之中,
的算法是最好的一个。这个算法使用了大量的数据来确
定农历月份和节气的分部,它仅实用于公历 1901 年到 2100 年之间的
200 年。
中国农历计算程式
跟据 提供的算法,我写了下面这个程式:
/**
*
* Copyright (c) 1997-2002 by Dr. Herong Yang
* 中国农历算法 - 实用于公历 1901 年至 2100 年之间的 200 年
*/
import .*;
import .*;
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 (>0) cmd = arg[0];
if (>1) y = nt(arg[1]);
发布者:admin,转转请注明出处:http://www.yc00.com/web/1710390068a1748744.html
评论列表(0条)