In. system. globalization. chineselunisolarcalendar is a calendar class for China. It is used to calculate information about the calendar, such as the calendar and calendar year of the traditional Chinese calendar.
Use
static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();
Ccalendar. maxsupporteddatetime returns the supported maximum date, that is, 2101-1-28
Ccalendar. minsupporteddatetime returns the supported minimum date, that is
Next we will convert the Gregorian calendar to the lunar calendar.
/// <Summary> /// obtain the lunar date based on the Gregorian calendar /// </Summary> /// <Param name = "datetime"> Gregorian date </param> // /<returns> </returns> Public static string getchinesedatetime (datetime) {int lyear = ccalendar. getyear (datetime); int lmonth = ccalendar. getmonth (datetime); int lday = ccalendar. getdayofmonth (datetime); // get the leap month. If the value is 0, the leapmonth int leapmonth = ccalendar is not displayed. getleapmonth (lyear); bool isleap = false; If (leapmonth> 0) {I F (leapmonth = lmonth) {// leap = true; lmonth --;} else if (lmonth> leapmonth) {lmonth --;} return string. concat (getlunisolaryear (lyear), "year", isleap? "Interval": String. Empty, getlunisolarmonth (lmonth), "month", getlunisolarday (lday ));
}
Test results:
Input Date: 2010-3-4
Return to lunar calendar: Geng Yin, September 1, lunar January 19
It can meet simple requirements.
Other code is also attached:
# Region agricultural calendar year // <summary> // ten-day dry // </Summary> Private Static string [] tiangan = {"A", "B ", "C", "ding", "E", "Ji", "Geng", "Xin", "Yi", "Xi "}; /// <summary> /// 12th branch // </Summary> Private Static string [] dizhi = {"sub", "ugly", "", "Mao", "Chen", "Si", "Wu", "wei", "shen", "", "Wu", "Hai "}; /// <summary> /// Zodiac /// </Summary> Private Static string [] shengxiao = {"rat", "Ox", "Tiger ", "free", "dragon", "snake", "horse", "goat", "monkey", "chicken", "dog", "Swine "}; /// < Summary> /// return the year of the calendar month. // </Summary> // <Param name = "year"> calendar year </param> // <returns> </returns> Public static string getlunisolaryear (INT year) {If (Year> 3) {int tgindex = (year-4) % 10; int dzindex = (year-4) % 12; return string. concat (tiangan [tgindex], dizhi [dzindex], "[", shengxiao [dzindex], "]");} Throw new argumentoutofrangeexception ("invalid year! ") ;}# Endregion
# Region lunar month // <summary> // lunar month // </Summary> Private Static string [] months = {"zheng", "2 ", "3", "4", "5", "6", "7", "8", "9", "10", "11 ", "Twelve (wax )"}; /// <summary> /// returns the lunar month /// </Summary> /// <Param name = "month"> month </param> /// <returns> </returns> Public static string getlunisolarmonth (INT month) {If (month <13 & month> 0) {return months [month-1];} Throw new argumentoutofrangeexception ("invalid month! ") ;}# Endregion
# Region lunar day // <summary> //// </Summary> Private Static string [] days1 = {"", "10", "Hangzhou ", "3"}; // <summary> // day // </Summary> Private Static string [] days = {"1", "2 ", "3", "4", "5", "6", "7", "8", "9", "10 "}; /// <summary> /// return to the lunar calendar day /// </Summary> /// <Param name = "day"> </param> /// <returns> </returns> Public static string getlunisolarday (INT Day) {If (day> 0 & day <32) {If (day! = 20 & day! = 30) {return string. concat (days1 [(day-1)/10], days [(day-1) % 10]);} else {return string. concat (days [(day-1)/10], days1 [1]) ;}} throw new argumentoutofrangeexception ("invalid day! ") ;}# Endregion
There is also a code to get the Zodiac by date:
/// <Summary> /// returns the Chinese zodiac // </Summary> /// <Param name = "datetime"> calendar date </param> /// <returns> </returns> Public static string getshengxiao (datetime) {return shengxiao [ccalendar. getterrestrialbranch (ccalendar. getsexagenaryyear (datetime)-1];}
Everything becomes simple.