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

最近郁闷地发现网上现有的相当一部分万年历上干支纪年的算法都是错误的。因为干支纪年是针对阴历而言的,而生肖属相又跟地支对应,所以元旦和春节之间那段时间在干支纪年法中应该归上一年,以阳历2007年2月9日为例,当日的阴历日期是二〇〇六年十二月廿二日,是丙戌年,即狗年,但是浏览一下目前的万年历,相当一部分都显示成了丁亥年,猪年,比较郁闷~~

然后就写了一个阴历阳历互相转化的类。

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

相关代码如下:


/**/ ///<summary>
///中国日历信息实体类
///cncxz(虫虫)2007-2-9
///</summary>

public sealed class ChineseCalendarInfo
{
privateDateTimem_SolarDate;
privateintm_LunarYear,m_LunarMonth,m_LunarDay;
privateboolm_IsLeapMonth=false;
privatestringm_LunarYearSexagenary=null,m_LunarYearAnimal=null;
privatestringm_LunarYearText=null,m_LunarMonthText=null,m_LunarDayText=null;
privatestringm_SolarWeekText=null,m_SolarConstellation=null,m_SolarBirthStone=null;

构造函数#region构造函数

publicChineseCalendarInfo()
:
this(DateTime.Now.Date)
{

}


/**////<summary>
///从指定的阳历日期创建中国日历信息实体类
///</summary>
///<paramname="date">指定的阳历日期</param>

publicChineseCalendarInfo(DateTimedate)
{
m_SolarDate
=date;
LoadFromSolarDate();
}


privatevoidLoadFromSolarDate()
{
m_IsLeapMonth
=false;
m_LunarYearSexagenary
=null;
m_LunarYearAnimal
=null;
m_LunarYearText
=null;
m_LunarMonthText
=null;
m_LunarDayText
=null;
m_SolarWeekText
=null;
m_SolarConstellation
=null;
m_SolarBirthStone
=null;

m_LunarYear
=calendar.GetYear(m_SolarDate);
m_LunarMonth
=calendar.GetMonth(m_SolarDate);
intleapMonth=calendar.GetLeapMonth(m_LunarYear);

if(leapMonth==m_LunarMonth)
{
m_IsLeapMonth
=true;
m_LunarMonth
-=1;
}

elseif(leapMonth>0&&leapMonth<m_LunarMonth)
{
m_LunarMonth
-=1;
}


m_LunarDay
=calendar.GetDayOfMonth(m_SolarDate);

CalcConstellation(m_SolarDate,
outm_SolarConstellation,outm_SolarBirthStone);
}


#endregion


日历属性#region日历属性

/**////<summary>
///阳历日期
///</summary>

publicDateTimeSolarDate
{
get{returnm_SolarDate;}
set
{
if(m_SolarDate.Equals(value))
return;
m_SolarDate
=value;
LoadFromSolarDate();
}

}

/**////<summary>
///星期几
///</summary>

publicstringSolarWeekText
{
get
{
if(string.IsNullOrEmpty(m_SolarWeekText))
{
inti=(int)m_SolarDate.DayOfWeek;
m_SolarWeekText
=ChineseWeekName[i];
}

returnm_SolarWeekText;
}

}

/**////<summary>
///阳历星座
///</summary>

publicstringSolarConstellation
{
get{returnm_SolarConstellation;}
}

/**////<summary>
///阳历诞生石
///</summary>

publicstringSolarBirthStone
{
get{returnm_SolarBirthStone;}
}


/**////<summary>
///阴历年份
///</summary>

publicintLunarYear
{
get{returnm_LunarYear;}
}

/**////<summary>
///阴历月份
///</summary>

publicintLunarMonth
{
get{returnm_LunarMonth;}
}

/**////<summary>
///是否阴历闰月
///</summary>

publicboolIsLeapMonth
{
get{returnm_IsLeapMonth;}
}

/**////<summary>
///阴历月中日期
///</summary>

publicintLunarDay
{
get{returnm_LunarDay;}
}


/**////<summary>
///阴历年干支
///</summary>

publicstringLunarYearSexagenary
{
get
{
if(string.IsNullOrEmpty(m_LunarYearSexagenary))
{
inty=calendar.GetSexagenaryYear(this.SolarDate);
m_LunarYearSexagenary
=CelestialStem.Substring((y-1)%10,1)+TerrestrialBranch.Substring((y-1)%12,1);
}

returnm_LunarYearSexagenary;
}

}

/**////<summary>
///阴历年生肖
///</summary>

publicstringLunarYearAnimal
{
get
{
if(string.IsNullOrEmpty(m_LunarYearAnimal))
{
inty=calendar.GetSexagenaryYear(this.SolarDate);
m_LunarYearAnimal
=Animals.Substring((y-1)%12,1);
}

returnm_LunarYearAnimal;
}

}



/**////<summary>
///阴历年文本
<img
分享到:
评论

你可能感兴趣的:(C++,c,算法,C#)