代碼說明:
1.說明:在東亞各國,除了通用的公元紀年之外,還有各自以前使用的陰曆紀年法,在.net2.0種增加了針對東亞各國的日曆類EastAsianLunisolarCalendar,
2.它是一個抽象類別,有各種針對不同國家的的子類,其中ChineseLunisolarCalendar就是針對中國的日曆類,它提公元紀年與中國傳統農曆紀年之間的相互轉換,並能計算今天是農曆幾月幾日星期幾。
3.利用它可以計算天干地支等有關農曆的資訊,本程式就是來簡單展示這個類的用法。它能計算的農曆範圍從西曆1901-2-19至2101-1-28。
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
namespace ChineseCalendar
{
public class Calendar
{
private static ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
static void Main(string[] args)
{
//ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
ShowYearInfo();
ShowCurrentYearInfo();
Console.ReadLine();
}
/// <summary>
/// 展示陰曆年份資訊
/// </summary>
public static void ShowYearInfo()
{
for (int i = chineseDate.MinSupportedDateTime.Year; i < chineseDate.MaxSupportedDateTime.Year; i++)
{
Console.WriteLine("年份:{0},月份總數:{1},總天數:{2},幹支序號:{3}", i, chineseDate.GetMonthsInYear(i),chineseDate.GetDaysInYear(i)
,chineseDate.GetSexagenaryYear(new DateTime(i,3,1)));
}
}
/// <summary>
/// 展示當前年份資訊
/// </summary>
public static void ShowCurrentYearInfo()
{
int lYear=chineseDate.GetYear(DateTime.Now);
int lMonth=chineseDate.GetMonth(DateTime.Now);
int lDay=chineseDate.GetDayOfMonth(DateTime.Now);
int leapMonth = chineseDate.GetLeapMonth(lYear);//擷取第幾個月是閏月,等於0表示本年無閏月
//如果今年有閏月
if (leapMonth > 0)
{
//閏月數等於當前月份
if (lMonth == leapMonth)
{
Console.WriteLine("今年的陰曆日期:{0}年閏{1}月{2}日。", lYear, lMonth - 1, lDay);
}
else if (lMonth > leapMonth)//
{
Console.WriteLine("今年的陰曆日期:{0}年{1}月{2}日。", lYear, lMonth - 1, lDay);
}
else
{
Console.WriteLine("今年的陰曆日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);
}
}
else
{
Console.WriteLine("今年的陰曆日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);
}
Console.WriteLine("今天的西曆日期:" + DateTime.Now.ToString("yyyy-MM-dd"));
Console.WriteLine("今年陰曆天數:{0},今年{1}閏年", chineseDate.GetDaysInYear(DateTime.Now.Year),(chineseDate.IsLeapYear(DateTime.Now.Year)==true)?"是":"不是");
Console.WriteLine("今年農曆每月的天數:");//注意:如果有13個數字表示當年有閏月
for (int i = 1; i <= chineseDate.GetMonthsInYear(DateTime.Now.Year); i++)
{
Console.Write("{0,-5}",chineseDate.GetDaysInMonth(DateTime.Now.Year,i));
}
}
}
}