您的位置:首页 > 编程语言 > C#

C#公历转农历

2010-04-27 08:31 316 查看
在.net2.0的版本中的System.Globalization.ChineseLunisolarCalendar是针对中国的日历类,公历与中国传统农历纪年之间的相互转换,利用它可以计算天干地支等有关农历的信息。

使用

staticChineseLunisolarCalendarcCalendar=newChineseLunisolarCalendar();

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

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


下面我们来实现公历转农历。

///<summary>
///根据公历获取农历日期
///</summary>
///<paramname="datetime">公历日期</param>
///<returns></returns>
publicstaticstringGetChineseDateTime(DateTimedatetime)
{
intlyear=cCalendar.GetYear(datetime);
intlmonth=cCalendar.GetMonth(datetime);
intlday=cCalendar.GetDayOfMonth(datetime);

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

boolisleap=false;

if(leapMonth>0)
{
if(leapMonth==lmonth)
{
//闰月
isleap=true;
lmonth--;
}
elseif(lmonth>leapMonth)
{
lmonth--;
}
}

returnstring.Concat(GetLunisolarYear(lyear),"年",isleap?"闰":string.Empty,GetLunisolarMonth(lmonth),"月",GetLunisolarDay(lday));
}

测试的结果:

传入日期:2010-3-4

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

可以满足简单的需求啦。

其他代码也附上:

#region农历年

///<summary>
///十天干
///</summary>
privatestaticstring[]tiangan={"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};

///<summary>
///十二地支
///</summary>
privatestaticstring[]dizhi={"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};

///<summary>
///十二生肖
///</summary>
privatestaticstring[]shengxiao={"鼠","牛","虎","免","龙","蛇","马","羊","猴","鸡","狗","猪"};

///<summary>
///返回农历天干地支年
///</summary>
///<paramname="year">农历年</param>
///<returns></returns>
publicstaticstringGetLunisolarYear(intyear)
{
if(year>3)
{
inttgIndex=(year-4)%10;
intdzIndex=(year-4)%12;

returnstring.Concat(tiangan[tgIndex],dizhi[dzIndex],"[",shengxiao[dzIndex],"]");

}

thrownewArgumentOutOfRangeException("无效的年份!");
}

#endregion


#region农历月

///<summary>
///农历月
///</summary>
privatestaticstring[]months={"正","二","三","四","五","六","七","八","九","十","十一","十二(腊)"};

///<summary>
///返回农历月
///</summary>
///<paramname="month">月份</param>
///<returns></returns>
publicstaticstringGetLunisolarMonth(intmonth)
{
if(month<13&&month>0)
{
returnmonths[month-1];
}

thrownewArgumentOutOfRangeException("无效的月份!");
}

#endregion


#region农历日

///<summary>
///
///</summary>
privatestaticstring[]days1={"初","十","廿","三"};

///<summary>
///日
///</summary>
privatestaticstring[]days={"一","二","三","四","五","六","七","八","九","十"};

///<summary>
///返回农历日
///</summary>
///<paramname="day"></param>
///<returns></returns>
publicstaticstringGetLunisolarDay(intday)
{
if(day>0&&day<32)
{
if(day!=20&&day!=30)
{
returnstring.Concat(days1[(day-1)/10],days[(day-1)%10]);
}
else
{
returnstring.Concat(days[(day-1)/10],days1[1]);
}
}

thrownewArgumentOutOfRangeException("无效的日!");
}

#endregion


还有一个根据日期获取生肖的代码:

///<summary>
///返回生肖
///</summary>
///<paramname="datetime">公历日期</param>
///<returns></returns>
publicstaticstringGetShengXiao(DateTimedatetime)
{
returnshengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime))-1];
}

source:http://www.cnblogs.com/huxj/archive/2010/03/04/1678160.html


阅读全文

类别:Net查看评论
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: