版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、中國(guó)公歷算法中國(guó)公歷算法不是太難,關(guān)鍵是星期值的確定。這里給出了簡(jiǎn)單算法: public static int dayOfWeek(int y, int m, int d) int w = 1; / 公歷一年一月一日是星期一,所以起始值為星期日 y = (y-1)%400 + 1; / 公歷星期值分部 400 年循環(huán)一次 int ly = (y-1)/4; / 閏年次數(shù) ly = ly - (y-1)/100; ly = ly + (y-1)/400; int ry = y - 1 - ly; / 常年次數(shù) w = w + ry; / 常年星期值增一 w = w + 2*ly; / 閏年星期值
2、增二 w = w + dayOfYear(y,m,d); w = (w-1)%7 + 1; return w; 中國(guó)農(nóng)歷算法根公歷相比,中國(guó)農(nóng)歷的算法相當(dāng)復(fù)雜。我在網(wǎng)上找的算法之中, 的算法是最好的一個(gè)。這個(gè)算法使用了大量的數(shù)據(jù)來(lái)確定農(nóng)歷月份和節(jié)氣的分部,它僅實(shí)用于公歷 1901 年到 2100 年之間的 200 年。中國(guó)農(nóng)歷計(jì)算程式跟據(jù) 提供的算法,我寫(xiě)了下面這個(gè)程式:HTML/* ChineseCalendarGB.java* Copyright (c) 1997-2002 by Dr. Herong Yang. * 中國(guó)農(nóng)歷算法 - 實(shí)用于公歷 1901 年至 2100 年之間的 200
3、 年*/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; / 負(fù)數(shù)表示閏月 p
4、rivate 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 anim
5、alNames = 鼠,牛,虎,兔,龍,蛇,馬,羊,猴,雞,狗,豬; public static void main(String arg) ChineseCalendarGB c = new ChineseCalendarGB(); String cmd = day; int y = 1901; int m = 1; int d = 1; if (arg.length0) cmd = arg0; if (arg.length1) y = Integer.parseInt(arg1); if (arg.length2) m = Integer.parseInt(arg); if (arg.le
6、ngth3) d = Integer.parseInt(arg); c.setGregorian(y,m,d); puteChineseFields(); puteSolarTerms(); 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
7、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
8、; 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 = daysInGregorianM
9、onthm-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 +
10、 1; / 公歷星期值分部 400 年循環(huán)一次 int ly = (y-1)/4; / 閏年次數(shù) ly = ly - (y-1)/100; ly = ly + (y-1)/400; int ry = y - 1 - ly; / 常年次數(shù) w = w + ry; / 常年星期值增一 w = w + 2*ly; / 閏年星期值增二 w = w + dayOfYear(y,m,d); w = (w-1)%7 + 1; return w; private static char chineseMonths = / 農(nóng)歷月份大小壓縮表,兩個(gè)字節(jié)表示一年。兩個(gè)字節(jié)共十六個(gè)二進(jìn)制位數(shù), / 前四個(gè)位數(shù)表示閏
11、月月份,后十二個(gè)位數(shù)表示十二個(gè)農(nóng)歷月份的大小。 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,0x5
12、6,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,0x0
13、2,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
14、,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,
15、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,0
16、x02,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, 0
17、x4a,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 ; / 初始日,公歷農(nóng)歷對(duì)應(yīng)日期: / 公歷 1901 年 1 月 1 日,
18、對(duì)應(yīng)農(nóng)歷 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 co
19、mputeChineseFields() if (gregorianYear2100) return 1; int startYear = baseYear; int startMonth = baseMonth; int startDate = baseDate; chineseYear = baseChineseYear; chineseMonth = baseChineseMonth; chineseDate = baseChineseDate; / 第二個(gè)對(duì)應(yīng)日,用以提高計(jì)算效率 / 公歷 2000 年 1 月 1 日,對(duì)應(yīng)農(nóng)歷 4697 年 11 月 25 日 if (gregori
20、anYear = 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 += days
21、InGregorianMonth(gregorianYear,i); daysDiff += gregorianDate - startDate; chineseDate += daysDiff; int lastDate = daysInChineseMonth(chineseYear, chineseMonth); int nextMonth = nextChineseMonth(chineseYear, chineseMonth); while (chineseDatelastDate) if (Math.abs(nextMonth) chineseMonth = nextMonth; chineseDate -= lastDate; lastDate = daysInChineseMonth(chineseYear, chineseMonth); nextMonth = nextChineseMonth(chineseYear, chineseMon
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣東外語(yǔ)外貿(mào)大學(xué)《納稅申報(bào)模擬實(shí)訓(xùn)》2023-2024學(xué)年第一學(xué)期期末試卷
- 廣東女子職業(yè)技術(shù)學(xué)院《工程制圖(Ⅱ)》2023-2024學(xué)年第一學(xué)期期末試卷
- 廣東南華工商職業(yè)學(xué)院《光纖器件工程》2023-2024學(xué)年第一學(xué)期期末試卷
- 廣東梅州職業(yè)技術(shù)學(xué)院《版面文化與設(shè)計(jì)》2023-2024學(xué)年第一學(xué)期期末試卷
- 專題三-信守合同與違約單元基礎(chǔ)測(cè)試2(新人教版選修5)
- 大學(xué)英語(yǔ)過(guò)程寫(xiě)作(吉林大學(xué))學(xué)習(xí)通測(cè)試及答案
- 個(gè)人理財(cái)規(guī)劃(重慶理工大學(xué))學(xué)習(xí)通測(cè)試及答案
- 【KS5U發(fā)布】河南省豫南九校2021屆高三上期第二次聯(lián)考-政治-掃描版含答案
- 2021年高考藝體生文化課百日沖刺-英語(yǔ)專題復(fù)習(xí)講練:專題二-代詞和介詞
- 江西省宜春市2024-2025學(xué)年上高二中高一(上)第三次月考物理試卷(含答案)
- 中國(guó)馬克思主義與當(dāng)代知到章節(jié)答案智慧樹(shù)2023年西安交通大學(xué)
- 組織協(xié)同運(yùn)用平衡計(jì)分卡創(chuàng)造企業(yè)合力
- 車輛剮蹭自愿和解協(xié)議書(shū)模板
- 兒科課件過(guò)敏性紫癜
- 學(xué)校安全事故應(yīng)急處置流程圖
- 姜安《政治學(xué)概論》(第2版)筆記和典型題(含考研真題)詳解
- 漢字拼寫(xiě)游戲
- 廣東省國(guó)家公務(wù)員錄用體檢表
- GB/T 12310-2012感官分析方法成對(duì)比較檢驗(yàn)
- 公廁管理保潔及糞便收運(yùn)處理方案
- FZ/T 70010-2006針織物平方米干燥重量的測(cè)定
評(píng)論
0/150
提交評(píng)論