一个阴历阳历互相转化的类(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>
///阴历年文本
///</summary>

publicstringLunarYearText
{
get
{
if(string.IsNullOrEmpty(m_LunarYearText))
{
m_LunarYearText
=Animals.Substring(calendar.GetSexagenaryYear(newDateTime(m_LunarYear,1,1))%12-1,1);
StringBuildersb
=newStringBuilder();
intyear=this.LunarYear;
intd;
do
{
d
=year%10;
sb.Insert(
0,ChineseNumber[d]);
year
=year/10;
}
while(year>0);
m_LunarYearText
=sb.ToString();
}

returnm_LunarYearText;
}

}

/**////<summary>
///阴历月文本
///</summary>

publicstringLunarMonthText
{
get
{
if(string.IsNullOrEmpty(m_LunarMonthText))
{
m_LunarMonthText
=(this.IsLeapMonth?"":"")+ChineseMonthName[this.LunarMonth-1];
}

returnm_LunarMonthText;
}

}


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

publicstringLunarDayText
{
get
{
if(string.IsNullOrEmpty(m_LunarDayText))
m_LunarDayText
=ChineseDayName[this.LunarDay-1];
returnm_LunarDayText;
}

}


#endregion


/**////<summary>
///根据指定阳历日期计算星座&诞生石
///</summary>
///<paramname="date">指定阳历日期</param>
///<paramname="constellation">星座</param>
///<paramname="birthstone">诞生石</param>

publicstaticvoidCalcConstellation(DateTimedate,outstringconstellation,outstringbirthstone)
{
inti=Convert.ToInt32(date.ToString("MMdd"));
intj;
if(i>=321&&i<=419)
j
=0;
elseif(i>=420&&i<=520)
j
=1;
elseif(i>=521&&i<=621)
j
=2;
elseif(i>=622&&i<=722)
j
=3;
elseif(i>=723&&i<=822)
j
=4;
elseif(i>=823&&i<=922)
j
=5;
elseif(i>=923&&i<=1023)
j
=6;
elseif(i>=1024&&i<=1121)
j
=7;
elseif(i>=1122&&i<=1221)
j
=8;
elseif(i>=1222||i<=119)
j
=9;
elseif(i>=120&&i<=218)
j
=10;
elseif(i>=219&&i<=320)
j
=11;
else
{
constellation
="未知星座";
birthstone
="未知诞生石";
return;
}

constellation
=Constellations[j];
birthstone
=BirthStones[j];

星座划分#region星座划分
//白羊座:3月21日------4月19日诞生石:钻石
//金牛座:4月20日------5月20日诞生石:蓝宝石
//双子座:5月21日------6月21日诞生石:玛瑙
//巨蟹座:6月22日------7月22日诞生石:珍珠
//狮子座:7月23日------8月22日诞生石:红宝石
//处女座:8月23日------9月22日诞生石:红条纹玛瑙
//天秤座:9月23日------10月23日诞生石:蓝宝石
//天蝎座:10月24日-----11月21日诞生石:猫眼石
//射手座:11月22日-----12月21日诞生石:黄宝石
//摩羯座:12月22日-----1月19日诞生石:土耳其玉
//水瓶座:1月20日-----2月18日诞生石:紫水晶
//双鱼座:2月19日------3月20日诞生石:月长石,血石
#endregion

}



阴历转阳历#region阴历转阳历

/**////<summary>
///获取指定年份春节当日(正月初一)的阳历日期
///</summary>
///<paramname="year">指定的年份</param>

privatestaticDateTimeGetLunarNewYearDate(intyear)
{
DateTimedt
=newDateTime(year,1,1);
intcnYear=calendar.GetYear(dt);
intcnMonth=calendar.GetMonth(dt);

intnum1=0;
intnum2=calendar.IsLeapYear(cnYear)?13:12;

while(num2>=cnMonth)
{
num1
+=calendar.GetDaysInMonth(cnYear,num2--);
}


num1
=num1-calendar.GetDayOfMonth(dt)+1;
returndt.AddDays(num1);
}


/**////<summary>
///阴历转阳历
///</summary>
///<paramname="year">阴历年</param>
///<paramname="month">阴历月</param>
///<paramname="day">阴历日</param>
///<paramname="IsLeapMonth">是否闰月</param>

publicstaticDateTimeGetDateFromLunarDate(intyear,intmonth,intday,boolIsLeapMonth)
{
if(year<1902||year>2100)
thrownewException("只支持1902~2100期间的农历年");
if(month<1||month>12)
thrownewException("表示月份的数字必须在1~12之间");

if(day<1||day>calendar.GetDaysInMonth(year,month))
thrownewException("农历日期输入有误");

intnum1=0,num2=0;
intleapMonth=calendar.GetLeapMonth(year);

if(((leapMonth==month+1)&&IsLeapMonth)||(leapMonth>0&&leapMonth<=month))
num2
=month;
else
num2
=month-1;

while(num2>0)
{
num1
+=calendar.GetDaysInMonth(year,num2--);
}


DateTimedt
=GetLunarNewYearDate(year);
returndt.AddDays(num1+day-1);
}


/**////<summary>
///阴历转阳历
///</summary>
///<paramname="date">阴历日期</param>
///<paramname="IsLeapMonth">是否闰月</param>

publicstaticDateTimeGetDateFromLunarDate(DateTimedate,boolIsLeapMonth)
{
returnGetDateFromLunarDate(date.Year,date.Month,date.Day,IsLeapMonth);
}


#endregion


从阴历创建日历#region从阴历创建日历

/**////<summary>
///从阴历创建日历实体
///</summary>
///<paramname="year">阴历年</param>
///<paramname="month">阴历月</param>
///<paramname="day">阴历日</param>
///<paramname="IsLeapMonth">是否闰月</param>

publicstaticChineseCalendarInfoFromLunarDate(intyear,intmonth,intday,boolIsLeapMonth)
{
DateTimedt
=GetDateFromLunarDate(year,month,day,IsLeapMonth);
returnnewChineseCalendarInfo(dt);
}

/**////<summary>
///从阴历创建日历实体
///</summary>
///<paramname="date">阴历日期</param>
///<paramname="IsLeapMonth">是否闰月</param>

publicstaticChineseCalendarInfoFromLunarDate(DateTimedate,boolIsLeapMonth)
{
returnFromLunarDate(date.Year,date.Month,date.Day,IsLeapMonth);
}


/**////<summary>
///从阴历创建日历实体
///</summary>
///<paramname="date">表示阴历日期的8位数字,例如:20070209</param>
///<paramname="IsLeapMonth">是否闰月</param>

publicstaticChineseCalendarInfoFromLunarDate(stringdate,boolIsLeapMonth)
{
Regexrg
=newSystem.Text.RegularExpressions.Regex(@"^/d{7}(/d)$");
Matchmc
=rg.Match(date);
if(!mc.Success)
{
thrownewException("日期字符串输入有误!");
}

DateTimedt
=DateTime.Parse(string.Format("{0}-{1}-{2}",date.Substring(0,4),date.Substring(4,2),date.Substring(6,2)));
returnFromLunarDate(dt,IsLeapMonth);
}



#endregion


privatestaticChineseLunisolarCalendarcalendar=newChineseLunisolarCalendar();
publicconststringChineseNumber="〇一二三四五六七八九";
publicconststringCelestialStem="甲乙丙丁戊己庚辛壬癸";
publicconststringTerrestrialBranch="子丑寅卯辰巳午未申酉戌亥";
publicconststringAnimals="鼠牛虎兔龙蛇马羊猴鸡狗猪";
publicstaticreadonlystring[]ChineseWeekName=newstring[]{"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
publicstaticreadonlystring[]ChineseDayName=newstring[]{
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"}
;
publicstaticreadonlystring[]ChineseMonthName=newstring[]{"","","","","","","","","","","十一","十二"};
publicstaticreadonlystring[]Constellations=newstring[]{"白羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天秤座","天蝎座","射手座","摩羯座","水瓶座","双鱼座"};
publicstaticreadonlystring[]BirthStones=newstring[]{"钻石","蓝宝石","玛瑙","珍珠","红宝石","红条纹玛瑙","蓝宝石","猫眼石","黄宝石","土耳其玉","紫水晶","月长石,血石"};


}

工程文件下载: http://www.cnblogs.com/Files/cncxz/ChineseCalendar.rar

你可能感兴趣的:(C#)