龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

C#农历计算查询(公历与农历之间转换)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
.net2.0种已经提供了公历与农历之间进行转换功能。下面就以几个简单的例子展示它的用法。 using System; using System.Collections.Generic; using System.Text; using System.Globalization; /** *说明:在东亚各

.net2.0种已经提供了公历与农历之间进行转换功能。下面就以几个简单的例子展示它的用法。

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Globalization;   
  5.   
  6. /**  
  7.  * 说明:在东亚各国,除了通用的公元纪年之外,还有各自以前使用的阴历纪年法,在.net2.0种增加了针对东亚各国的日历类EastAsianLunisolarCalendar,  
  8.  * 它是一个抽象类,有各种针对不同国家的的子类,其中ChineseLunisolarCalendar就是针对中国的日历类,它提公元纪年与中国传统农历纪年之间的相互转换  
  9.  * 利用它可以计算天干地支等有关农历的信息,本程序就是来简单展示这个类的用法。它能计算的农历范围从公历1901-2-19至2101-1-28。  
  10.  * 作者:周公   
  11.  * 日期:2007-11-21  
  12.  * 最后维护日期:2009-07-28  
  13.  * 首发地址:http://blog.csdn.net/zhoufoxcn/archive/2007/11/21/1896258.aspx  
  14.  */  
  15. namespace ChineseCalendar   
  16. {   
  17.     class Calendar   
  18.     {   
  19.         private static ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();   
  20.         static void Main(string[] args)   
  21.         {   
  22.             //ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();   
  23.             ShowYearInfo();   
  24.             ShowCurrentYearInfo();   
  25.             Console.ReadLine();   
  26.         }   
  27.         /// <summary>   
  28.         /// 展示阴历年份信息   
  29.         /// </summary>   
  30.         public static void ShowYearInfo()   
  31.         {   
  32.             for (int i = chineseDate.MinSupportedDateTime.Year; i < chineseDate.MaxSupportedDateTime.Year; i++)   
  33.             {   
  34.                 Console.WriteLine("年份:{0},月份总数:{1},总天数:{2},干支序号:{3}", i, chineseDate.GetMonthsInYear(i),chineseDate.GetDaysInYear(i)   
  35.                     ,chineseDate.GetSexagenaryYear(new DateTime(i,3,1)));   
  36.             }   
  37.         }   
  38.         /// <summary>   
  39.         /// 展示当前年份信息   
  40.         /// </summary>   
  41.         public static void ShowCurrentYearInfo()   
  42.         {   
  43.             /* GetLeapMonth(int year)方法返回一个1到13之间的数字,  
  44.              * 比如:1、该年阴历2月有闰月,则返回3  
  45.              * 如果:2、该年阴历8月有闰月,则返回9  
  46.              * GetMonth(DateTime dateTime)返回是当前月份,忽略是否闰月  
  47.              * 比如:1、该年阴历2月有闰月,2月返回2,闰2月返回3  
  48.              * 如果:2、该年阴历8月有闰月,8月返回8,闰8月返回9  
  49.              */  
  50.             int leapMonth = chineseDate.GetLeapMonth(DateTime.Now.Year);//获取第几个月是闰月,等于0表示本年无闰月   
  51.             int lYear=chineseDate.GetYear(DateTime.Now);   
  52.             int lMonth=chineseDate.GetMonth(DateTime.Now);   
  53.             int lDay=chineseDate.GetDayOfMonth(DateTime.Now);   
  54.             //如果今年有闰月   
  55.             if (leapMonth > 0)   
  56.             {   
  57.                 //闰月数等于当前月份   
  58.                 if (lMonth == leapMonth)   
  59.                 {   
  60.                     Console.WriteLine("今年的阴历日期:{0}年闰{1}月{2}日。", lYear, lMonth - 1, lDay);   
  61.                 }   
  62.                 else if (lMonth > leapMonth)//   
  63.                 {   
  64.                     Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth - 1, lDay);   
  65.                 }   
  66.                 else  
  67.                 {   
  68.                     Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);   
  69.                 }   
  70.                    
  71.             }   
  72.             else  
  73.             {   
  74.                 Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);   
  75.             }   
  76.             Console.WriteLine("今天的公历日期:" + DateTime.Now.ToString("yyyy-MM-dd"));   
  77.             Console.WriteLine("今年阴历天数:{0},今年{1}闰年", chineseDate.GetDaysInYear(DateTime.Now.Year),(chineseDate.IsLeapYear(DateTime.Now.Year)==true)?"是":"不是");   
  78.   
  79.             Console.WriteLine("今年农历每月的天数:");//注意:如果有13个数字表示当年有闰月   
  80.             for (int i = 1; i <= chineseDate.GetMonthsInYear(DateTime.Now.Year); i++)   
  81.             {   
  82.                 Console.Write("{0,-5}",chineseDate.GetDaysInMonth(DateTime.Now.Year,i));   
  83.             }   
  84.         }   
  85.     }   


精彩图集

赞助商链接