使用ChineseLunisolarCalendar类转换公历和农历日期

本文介绍如何使用System.Globalization.ChineseLunisolarCalendar类进行公历与农历之间的转换,并提供了一个具体的示例代码,包括如何获取天干地支、生肖及具体的农历日期。
System.Globalization.ChineseLunisolarCalendar 是针对中国的日历类,公历与中国传统农历纪年之间的相互转换,利用它可以计算天干地支等有关农历的信息。

使用

static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();

cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28

cCalendar.MinSupportedDateTime  返回支持的最小日期,即1901-2-19

下面我们来实现公历转农历。
  1. /// <summary>
  2. /// 根据公历获取农历日期
  3. /// </summary>
  4. /// <param name="datetime">公历日期</param>
  5. /// <returns></returns>
  6. public static string GetChineseDateTime(DateTime datetime)
  7. {
  8.     int lyear = cCalendar.GetYear(datetime);
  9.     int lmonth = cCalendar.GetMonth(datetime);
  10.     int lday = cCalendar.GetDayOfMonth(datetime);

  11.     //获取闰月, 0 则表示没有闰月
  12.     int leapMonth = cCalendar.GetLeapMonth(lyear);

  13.     bool isleap = false;

  14.     if (leapMonth > 0)
  15.     {
  16.         if (leapMonth == lmonth)
  17.         {
  18.             //闰月
  19.             isleap = true;
  20.             lmonth--;
  21.         }
  22.         else if (lmonth > leapMonth)
  23.         {
  24.             lmonth--;
  25.         }
  26.     }

  27.     return string.Concat(GetLunisolarYear(datetime.Year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday));
  28. }
复制代码
测试的结果:

传入日期:2010-3-4

返回农历:庚寅[虎]年正月十九

可以满足简单的需求啦。

其他代码也附上:
  1. #region 农历年

  2. /// <summary>
  3. /// 十天干
  4. /// </summary>
  5. private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

  6. /// <summary>
  7. /// 十二地支
  8. /// </summary>
  9. private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

  10. /// <summary>
  11. /// 十二生肖
  12. /// </summary>
  13. private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

  14. /// <summary>
  15. /// 返回农历天干地支年 
  16. /// </summary>
  17. /// <param name="year">公历年</param>
  18. /// <returns></returns>
  19. public static string GetLunisolarYear(int year)
  20. {
  21.     if (year > 3)
  22.     {
  23.         int tgIndex = (year - 4) % 10;
  24.         int dzIndex = (year - 4) % 12;

  25.         return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "[", shengxiao[dzIndex], "]");

  26.     }

  27.     throw new ArgumentOutOfRangeException("无效的年份!");
  28. }      
  29. #endregion

  30. #region 农历月

  31. /// <summary>
  32. /// 农历月
  33. /// </summary>
  34. private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
  35. /// <summary>
  36. /// 返回农历月
  37. /// </summary>
  38. /// <param name="month">月份</param>
  39. /// <returns></returns>
  40. public static string GetLunisolarMonth(int month)
  41. {
  42.     if (month < 13 && month > 0)
  43.     {
  44.         return months[month - 1];
  45.     }

  46.     throw new ArgumentOutOfRangeException("无效的月份!");
  47. }
  48. #endregion

  49. #region 农历日

  50. /// <summary>
  51. /// 
  52. /// </summary>
  53. private static string[] days1 = { "初", "十", "廿", "三" };

  54. /// <summary>
  55. /// 日
  56. /// </summary>
  57. private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };

  58. /// <summary>
  59. /// 返回农历日
  60. /// </summary>
  61. /// <param name="day"></param>
  62. /// <returns></returns>
  63. public static string GetLunisolarDay(int day)
  64. {
  65.     if (day > 0 && day < 32)
  66.     {
  67.         if (day != 20 && day != 30)
  68.         {
  69.             return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
  70.         }
  71.         else
  72.         {
  73.             return string.Concat(days[day / 10], days1[1]);
  74.         }
  75.     }

  76.     throw new ArgumentOutOfRangeException("无效的日!");
  77. }

  78. #endregion
复制代码
还有一个根据日期获取生肖的代码:
  1. /// <summary>
  2. /// 返回生肖
  3. /// </summary>
  4. /// <param name="datetime">公历日期</param>
  5. /// <returns></returns>
  6. public static string GetShengXiao(DateTime datetime)
  7. {
  8.     return shengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime)) - 1];
  9. }
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值