gds通用软件开发系统

导航

一个阴历阳历互相转化的类(C#源码)

  1. /// <summary>
  2. /// 中国日历信息实体类
  3. /// cncxz(虫虫) 2007-2-9
  4. /// </summary>
  5. public sealed class ChineseCalendarInfo  
  6. {  
  7. private DateTime m_SolarDate;  
  8. private int m_LunarYear, m_LunarMonth, m_LunarDay;  
  9. private bool m_IsLeapMonth = false;  
  10. private string m_LunarYearSexagenary = null, m_LunarYearAnimal = null;  
  11. private string m_LunarYearText = null, m_LunarMonthText = null, m_LunarDayText = null;  
  12. private string m_SolarWeekText = null, m_SolarConstellation = null, m_SolarBirthStone = null;  
  13.     #region 构造函数
  14. public ChineseCalendarInfo()  
  15.         : this(DateTime.Now.Date)  
  16.     {  
  17.     }  
  18. /// <summary>
  19. /// 从指定的阳历日期创建中国日历信息实体类
  20. /// </summary>
  21. /// <param name="date">指定的阳历日期</param>
  22. public ChineseCalendarInfo(DateTime date)  
  23.     {  
  24.         m_SolarDate = date;  
  25.         LoadFromSolarDate();  
  26.     }  
  27. private void LoadFromSolarDate()  
  28.     {  
  29.         m_IsLeapMonth = false;  
  30.         m_LunarYearSexagenary = null;  
  31.         m_LunarYearAnimal = null;  
  32.         m_LunarYearText = null;  
  33.         m_LunarMonthText = null;  
  34.         m_LunarDayText = null;  
  35.         m_SolarWeekText = null;  
  36.         m_SolarConstellation = null;  
  37.         m_SolarBirthStone = null;  
  38.         m_LunarYear = calendar.GetYear(m_SolarDate);  
  39.         m_LunarMonth = calendar.GetMonth(m_SolarDate);  
  40. int leapMonth = calendar.GetLeapMonth(m_LunarYear);  
  41. if (leapMonth == m_LunarMonth)  
  42.         {  
  43.             m_IsLeapMonth = true;  
  44.             m_LunarMonth -= 1;  
  45.         }  
  46. else if (leapMonth > 0 && leapMonth < m_LunarMonth)  
  47.         {  
  48.             m_LunarMonth -= 1;  
  49.         }  
  50.         m_LunarDay = calendar.GetDayOfMonth(m_SolarDate);  
  51.         CalcConstellation(m_SolarDate, out m_SolarConstellation, out m_SolarBirthStone);  
  52.     }  
  53.     #endregion
  54.     #region 日历属性
  55. /// <summary>
  56. /// 阳历日期
  57. /// </summary>
  58. public DateTime SolarDate  
  59.     {  
  60. get { return m_SolarDate; }  
  61. set
  62.         {  
  63. if (m_SolarDate.Equals(value))  
  64. return;  
  65.             m_SolarDate = value;  
  66.             LoadFromSolarDate();  
  67.         }  
  68.     }  
  69. /// <summary>
  70. /// 星期几
  71. /// </summary>
  72. public string SolarWeekText  
  73.     {  
  74. get
  75.         {  
  76. if (string.IsNullOrEmpty(m_SolarWeekText))  
  77.             {  
  78. int i = (int) m_SolarDate.DayOfWeek;  
  79.                 m_SolarWeekText = ChineseWeekName[i];  
  80.             }  
  81. return m_SolarWeekText;  
  82.         }  
  83.     }  
  84. /// <summary>
  85. /// 阳历星座
  86. /// </summary>
  87. public string SolarConstellation  
  88.     {  
  89. get { return m_SolarConstellation; }  
  90.     }  
  91. /// <summary>
  92. /// 阳历诞生石
  93. /// </summary>
  94. public string SolarBirthStone  
  95.     {  
  96. get { return m_SolarBirthStone; }  
  97.     }  
  98. /// <summary>
  99. /// 阴历年份
  100. /// </summary>
  101. public int LunarYear  
  102.     {  
  103. get { return m_LunarYear; }  
  104.     }  
  105. /// <summary>
  106. /// 阴历月份
  107. /// </summary>
  108. public int LunarMonth  
  109.     {  
  110. get { return m_LunarMonth; }  
  111.     }  
  112. /// <summary>
  113. /// 是否阴历闰月
  114. /// </summary>
  115. public bool IsLeapMonth  
  116.     {  
  117. get { return m_IsLeapMonth; }  
  118.     }  
  119. /// <summary>
  120. /// 阴历月中日期
  121. /// </summary>
  122. public int LunarDay  
  123.     {  
  124. get { return m_LunarDay; }  
  125.     }  
  126. /// <summary>
  127. /// 阴历年干支
  128. /// </summary>
  129. public string LunarYearSexagenary  
  130.     {  
  131. get
  132.         {  
  133. if (string.IsNullOrEmpty(m_LunarYearSexagenary))  
  134.             {  
  135. int y = calendar.GetSexagenaryYear(this.SolarDate);  
  136.                 m_LunarYearSexagenary = CelestialStem.Substring((y - 1) % 10, 1) + TerrestrialBranch.Substring((y - 1) % 12, 1);  
  137.             }  
  138. return m_LunarYearSexagenary;  
  139.         }  
  140.     }  
  141. /// <summary>
  142. /// 阴历年生肖
  143. /// </summary>
  144. public string LunarYearAnimal  
  145.     {  
  146. get
  147.         {  
  148. if (string.IsNullOrEmpty(m_LunarYearAnimal))  
  149.             {  
  150. int y = calendar.GetSexagenaryYear(this.SolarDate);  
  151.                 m_LunarYearAnimal = Animals.Substring((y - 1) % 12, 1);  
  152.             }  
  153. return m_LunarYearAnimal;  
  154.         }  
  155.     }  
  156. /// <summary>
  157. /// 阴历年文本
  158. /// </summary>
  159. public string LunarYearText  
  160.     {  
  161. get
  162.         {  
  163. if (string.IsNullOrEmpty(m_LunarYearText))  
  164.             {  
  165.                 m_LunarYearText = Animals.Substring(calendar.GetSexagenaryYear(new DateTime(m_LunarYear, 1, 1)) % 12 - 1, 1);  
  166.                 StringBuilder sb = new StringBuilder();  
  167. int year = this.LunarYear;  
  168. int d;  
  169. do
  170.                 {  
  171.                     d = year % 10;  
  172.                     sb.Insert(0, ChineseNumber[d]);  
  173.                     year = year / 10;  
  174.                 } while (year > 0);  
  175.                 m_LunarYearText = sb.ToString();  
  176.             }  
  177. return m_LunarYearText;  
  178.         }  
  179.     }  
  180. /// <summary>
  181. /// 阴历月文本
  182. /// </summary>
  183. public string LunarMonthText  
  184.     {  
  185. get
  186.         {  
  187. if (string.IsNullOrEmpty(m_LunarMonthText))  
  188.             {  
  189.                 m_LunarMonthText = (this.IsLeapMonth ? "闰" : "") + ChineseMonthName[this.LunarMonth - 1];  
  190.             }  
  191. return m_LunarMonthText;  
  192.         }  
  193.     }  
  194. /// <summary>
  195. /// 阴历月中日期文本
  196. /// </summary>
  197. public string LunarDayText  
  198.     {  
  199. get
  200.         {  
  201. if (string.IsNullOrEmpty(m_LunarDayText))  
  202.                 m_LunarDayText = ChineseDayName[this.LunarDay - 1];  
  203. return m_LunarDayText;  
  204.         }  
  205.     }  
  206.     #endregion
  207. /// <summary>
  208. /// 根据指定阳历日期计算星座&诞生石
  209. /// </summary>
  210. /// <param name="date">指定阳历日期</param>
  211. /// <param name="constellation">星座</param>
  212. /// <param name="birthstone">诞生石</param>
  213. public static void CalcConstellation(DateTime date, out string constellation, out string birthstone)  
  214.     {  
  215. int i = Convert.ToInt32(date.ToString("MMdd"));  
  216. int j;  
  217. if (i >= 321 && i <= 419)  
  218.             j = 0;  
  219. else if (i >= 420 && i <= 520)  
  220.             j = 1;  
  221. else if (i >= 521 && i <= 621)  
  222.             j = 2;  
  223. else if (i >= 622 && i <= 722)  
  224.             j = 3;  
  225. else if (i >= 723 && i <= 822)  
  226.             j = 4;  
  227. else if (i >= 823 && i <= 922)  
  228.             j = 5;  
  229. else if (i >= 923 && i <= 1023)  
  230.             j = 6;  
  231. else if (i >= 1024 && i <= 1121)  
  232.             j = 7;  
  233. else if (i >= 1122 && i <= 1221)  
  234.             j = 8;  
  235. else if (i >= 1222 || i <= 119)  
  236.             j = 9;  
  237. else if (i >= 120 && i <= 218)  
  238.             j = 10;  
  239. else if (i >= 219 && i <= 320)  
  240.             j = 11;  
  241. else
  242.         {  
  243.             constellation = "未知星座";  
  244.             birthstone = "未知诞生石";  
  245. return;  
  246.         }  
  247.         constellation = Constellations[j];  
  248.         birthstone = BirthStones[j];  
  249.         #region 星座划分
  250. //白羊座:   3月21日------4月19日     诞生石:   钻石   
  251. //金牛座:   4月20日------5月20日   诞生石:   蓝宝石   
  252. //双子座:   5月21日------6月21日     诞生石:   玛瑙   
  253. //巨蟹座:   6月22日------7月22日   诞生石:   珍珠   
  254. //狮子座:   7月23日------8月22日   诞生石:   红宝石   
  255. //处女座:   8月23日------9月22日   诞生石:   红条纹玛瑙   
  256. //天秤座:   9月23日------10月23日     诞生石:   蓝宝石   
  257. //天蝎座:   10月24日-----11月21日     诞生石:   猫眼石   
  258. //射手座:   11月22日-----12月21日   诞生石:   黄宝石   
  259. //摩羯座:   12月22日-----1月19日   诞生石:   土耳其玉   
  260. //水瓶座:   1月20日-----2月18日   诞生石:   紫水晶   
  261. //双鱼座:   2月19日------3月20日   诞生石:   月长石,血石  
  262.         #endregion
  263.     }  
  264.     #region 阴历转阳历
  265. /// <summary>
  266. /// 获取指定年份春节当日(正月初一)的阳历日期
  267. /// </summary>
  268. /// <param name="year">指定的年份</param>
  269. private static DateTime GetLunarNewYearDate(int year)  
  270.     {  
  271.         DateTime dt = new DateTime(year, 1, 1);  
  272. int cnYear = calendar.GetYear(dt);  
  273. int cnMonth = calendar.GetMonth(dt);  
  274. int num1 = 0;  
  275. int num2 = calendar.IsLeapYear(cnYear) ? 13 : 12;  
  276. while (num2 >= cnMonth)  
  277.         {  
  278.             num1 += calendar.GetDaysInMonth(cnYear, num2--);  
  279.         }  
  280.         num1 = num1 - calendar.GetDayOfMonth(dt) + 1;  
  281. return dt.AddDays(num1);  
  282.     }  
  283. /// <summary>
  284. /// 阴历转阳历
  285. /// </summary>
  286. /// <param name="year">阴历年</param>
  287. /// <param name="month">阴历月</param>
  288. /// <param name="day">阴历日</param>
  289. /// <param name="IsLeapMonth">是否闰月</param>
  290. public static DateTime GetDateFromLunarDate(int year, int month, int day, bool IsLeapMonth)  
  291.     {  
  292. if (year < 1902 || year > 2100)  
  293. throw new Exception("只支持1902~2100期间的农历年");  
  294. if (month < 1 || month > 12)  
  295. throw new Exception("表示月份的数字必须在1~12之间");  
  296. if (day < 1 || day > calendar.GetDaysInMonth(year, month))  
  297. throw new Exception("农历日期输入有误");  
  298. int num1 = 0, num2 = 0;  
  299. int leapMonth = calendar.GetLeapMonth(year);  
  300. if (((leapMonth == month + 1) && IsLeapMonth) || (leapMonth > 0 && leapMonth <= month))  
  301.             num2 = month;  
  302. else
  303.             num2 = month - 1;  
  304. while (num2 > 0)  
  305.         {  
  306.             num1 += calendar.GetDaysInMonth(year, num2--);  
  307.         }  
  308.         DateTime dt = GetLunarNewYearDate(year);  
  309. return dt.AddDays(num1 + day - 1);  
  310.     }  
  311. /// <summary>
  312. /// 阴历转阳历
  313. /// </summary>
  314. /// <param name="date">阴历日期</param>
  315. /// <param name="IsLeapMonth">是否闰月</param>
  316. public static DateTime GetDateFromLunarDate(DateTime date, bool IsLeapMonth)  
  317.     {  
  318. return GetDateFromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth);  
  319.     }  
  320.     #endregion
  321.     #region 从阴历创建日历
  322. /// <summary>
  323. /// 从阴历创建日历实体
  324. /// </summary>
  325. /// <param name="year">阴历年</param>
  326. /// <param name="month">阴历月</param>
  327. /// <param name="day">阴历日</param>
  328. /// <param name="IsLeapMonth">是否闰月</param>
  329. public static ChineseCalendarInfo FromLunarDate(int year, int month, int day, bool IsLeapMonth)  
  330.     {  
  331.         DateTime dt = GetDateFromLunarDate(year, month, day, IsLeapMonth);  
  332. return new ChineseCalendarInfo(dt);  
  333.     }  
  334. /// <summary>
  335. /// 从阴历创建日历实体
  336. /// </summary>
  337. /// <param name="date">阴历日期</param>
  338. /// <param name="IsLeapMonth">是否闰月</param>
  339. public static ChineseCalendarInfo FromLunarDate(DateTime date, bool IsLeapMonth)  
  340.     {  
  341. return FromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth);  
  342.     }  
  343. /// <summary>
  344. /// 从阴历创建日历实体
  345. /// </summary>
  346. /// <param name="date">表示阴历日期的8位数字,例如:20070209</param>
  347. /// <param name="IsLeapMonth">是否闰月</param>
  348. public static ChineseCalendarInfo FromLunarDate(string date, bool IsLeapMonth)  
  349.     {  
  350.         Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{7}(\d)$");  
  351.         Match mc = rg.Match(date);  
  352. if (!mc.Success)  
  353.         {  
  354. throw new Exception("日期字符串输入有误!");  
  355.         }  
  356.         DateTime dt = DateTime.Parse(string.Format("{0}-{1}-{2}", date.Substring(0, 4), date.Substring(4, 2), date.Substring(6, 2)));  
  357. return FromLunarDate(dt, IsLeapMonth);  
  358.     }  
  359.     #endregion
  360. private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();  
  361. public const string ChineseNumber = "〇一二三四五六七八九";  
  362. public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";  
  363. public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";  
  364. public const string Animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";  
  365. public static readonly string[] ChineseWeekName   
  366.         = new string[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };  
  367. public static readonly string[] ChineseDayName = new string[] {  
  368. "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",  
  369. "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",  
  370. "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};  
  371. public static readonly string[] ChineseMonthName  
  372.         = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };  
  373. public static readonly string[] Constellations   
  374.         = new string[] { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座" };  
  375. public static readonly string[] BirthStones   
  376.         = new string[] { "钻石", "蓝宝石", "玛瑙", "珍珠", "红宝石", "红条纹玛瑙", "蓝宝石", "猫眼石", "黄宝石", "土耳其玉", "紫水晶", "月长石,血石" };  

posted on 2010-04-30 15:58  gds通用软件开发系统  阅读(781)  评论(0编辑  收藏  举报