您的位置:首页 > 编程语言 > Java开发

中国年历算法和程式-Java基础-Java-编程开发

2008-05-20 05:32 691 查看
google_ad_client = "pub-8800625213955058";

/* 336x280, 创建于 07-11-21 */

google_ad_slot = "0989131976";

google_ad_width = 336;

google_ad_height = 280;

//

中国年历算法和程式

本文摘自“和荣笔记 - 中国农历二百年算法及年历”

中国公历算法

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

public static int dayOfWeek(int y, int m, int d)
{ int w = 1;
// 公历一年一月一日是星期一,所以起始值为星期日 y = (y-1)@0   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[code]
中国二百年年历 1901 年至 2100 年

我用上面这个程式制作了二百年年历,1901 年至 2100 年,全部收录在这本书中。

年历格式说明:

农历日期列在公历日期后面。
节气用节气名称标明。
农历每月第一天用月份名称标明。
例如,2000 年一月的表达格式如下:
[HTML]
一月
日 一 二 三 四 五 六
1/25
2/26 3/27 4/28 5/29 立春 腊月 8/ 2
9/ 3 10/ 4 11/ 5 12/ 6 13/ 7 14/ 8 15/ 9
16/10 17/11 18/12 19/13 20/14 雨水 22/16
23/17 24/18 25/19 26/20 27/21 28/22 29/23
30/24 31/25
[/HTML]

其中:

"1/25" - 表示公历 1 号和农历 25 号。
"立春" - 表示节气。
"腊月" - 表示农历 12 月第一天。

------
有关中国年历算法和程式的详细注解和二百年年历,请参考
“和荣笔记 - 中国农历二百年算法及年历”
- http://www.herongyang.com/year_gb/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: