C#實(shí)現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類實(shí)例
本文實(shí)例講述了C#實(shí)現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類。分享給大家供大家參考,具體如下:
最近郁悶地發(fā)現(xiàn)網(wǎng)上現(xiàn)有的相當(dāng)一部分萬年歷上干支紀(jì)年的算法都是錯誤的。因?yàn)楦芍Ъo(jì)年是針對陰歷而言的,而生肖屬相又跟地支對應(yīng),所以元旦和春節(jié)之間那段時間在干支紀(jì)年法中應(yīng)該歸上一年,以陽歷2007年2月9日為例,當(dāng)日的陰歷日期是二〇〇六年十二月廿二日,是丙戌年,即狗年,但是瀏覽一下目前的萬年歷,相當(dāng)一部分都顯示成了丁亥年,豬年,比較郁悶~~
然后就寫了一個陰歷陽歷互相轉(zhuǎn)化的類。
相關(guān)代碼如下:
/// <summary> /// 中國日歷信息實(shí)體類 /// </summary> public sealed class ChineseCalendarInfo { private DateTime m_SolarDate; private int m_LunarYear, m_LunarMonth, m_LunarDay; private bool m_IsLeapMonth = false; private string m_LunarYearSexagenary = null, m_LunarYearAnimal = null; private string m_LunarYearText = null, m_LunarMonthText = null, m_LunarDayText = null; private string m_SolarWeekText = null, m_SolarConstellation = null, m_SolarBirthStone = null; 日歷屬性 /// <summary> /// 根據(jù)指定陽歷日期計(jì)算星座&誕生石 /// </summary> /// <param name="date">指定陽歷日期</param> /// <param name="constellation">星座</param> /// <param name="birthstone">誕生石</param> public static void CalcConstellation(DateTime date, out string constellation, out string birthstone) { int i = Convert.ToInt32(date.ToString("MMdd")); int j; if (i >= 321 && i <= 419) j = 0; else if (i >= 420 && i <= 520) j = 1; else if (i >= 521 && i <= 621) j = 2; else if (i >= 622 && i <= 722) j = 3; else if (i >= 723 && i <= 822) j = 4; else if (i >= 823 && i <= 922) j = 5; else if (i >= 923 && i <= 1023) j = 6; else if (i >= 1024 && i <= 1121) j = 7; else if (i >= 1122 && i <= 1221) j = 8; else if (i >= 1222 || i <= 119) j = 9; else if (i >= 120 && i <= 218) j = 10; else if (i >= 219 && i <= 320) j = 11; else { constellation = "未知星座"; birthstone = "未知誕生石"; return; } constellation = Constellations[j]; birthstone = BirthStones[j]; 星座劃分 } 陰歷轉(zhuǎn)陽歷 從陰歷創(chuàng)建日歷 private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar(); public const string ChineseNumber = "〇一二三四五六七八九"; public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸"; public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥"; public const string Animals = "鼠?;⑼谬埳唏R羊猴雞狗豬"; public static readonly string[] ChineseWeekName = new string[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; public static readonly string[] ChineseDayName = new string[] { "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十", "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"}; public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; public static readonly string[] Constellations = new string[] { "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "雙魚座" }; public static readonly string[] BirthStones = new string[] { "鉆石", "藍(lán)寶石", "瑪瑙", "珍珠", "紅寶石", "紅條紋瑪瑙", "藍(lán)寶石", "貓眼石", "黃寶石", "土耳其玉", "紫水晶", "月長石,血石" }; }
附:完整實(shí)例代碼點(diǎn)擊此處本站下載。
PS:這里再為大家推薦幾款日歷相關(guān)在線工具供大家參考:
網(wǎng)頁萬年歷日歷:
http://tools.jb51.net/bianmin/webwannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線萬年歷黃歷flash版:
http://tools.jb51.net/bianmin/flashwnl
另外,本站歷史上的今天也有相似的農(nóng)歷日期顯示功能:
http://tools.jb51.net/bianmin/lishi
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#日期與時間操作技巧總結(jié)》、《C#字符串操作技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn)
這篇文章主要介紹了C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02VS2015為console.readkey添加代碼片段的方法
這篇文章主要介紹了VS2015為console.readkey添加代碼片段的方法,需要的朋友可以參考下2016-12-12C#打印類PrintDocument、PrintDialog、PrintPreviewDialog使用示例
這篇文章主要介紹了C#打印類PrintDocument、PrintDialog、PrintPreviewDialog使用示例,本文分別給出了示例代碼,需要的朋友可以參考下2015-06-06C#實(shí)例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置,補(bǔ)抽
這篇文章主要介紹了C#實(shí)例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置,補(bǔ)抽 的相關(guān)資料,需要的朋友可以參考下2016-01-01C#使用AutoUpdater.NET實(shí)現(xiàn)程序自動更新
開發(fā)桌面應(yīng)用程序的時候,經(jīng)常會因?yàn)樾略龉δ苄枨蠡蛐迯?fù)已知問題,要求客戶更新應(yīng)用程序,在.Net體系中采用?AutoUpdater.NET?組件可以非常便捷的實(shí)現(xiàn)這一功能,需要的朋友可以參考下2024-02-02