实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | public static class ChinaDate { private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar(); private static Hashtable gHoliday = new Hashtable(); private static Hashtable nHoliday = new Hashtable(); private static string [] JQ = { "小寒" , "大寒" , "立春" , "雨水" , "惊蛰" , "春分" , "清明" , "谷雨" , "立夏" , "小满" , "芒种" , "夏至" , "小暑" , "大暑" , "立秋" , "处暑" , "白露" , "秋分" , "寒露" , "霜降" , "立冬" , "小雪" , "大雪" , "冬至" }; private static int [] JQData = { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 }; static ChinaDate() { //公历节日 gHoliday.Add( "0101" , "元旦" ); gHoliday.Add( "0214" , "情人节" ); gHoliday.Add( "0305" , "雷锋日" ); gHoliday.Add( "0308" , "妇女节" ); gHoliday.Add( "0312" , "植树节" ); gHoliday.Add( "0315" , "消权日" ); gHoliday.Add( "0401" , "愚人节" ); gHoliday.Add( "0501" , "劳动节" ); gHoliday.Add( "0504" , "青年节" ); gHoliday.Add( "0601" , "儿童节" ); gHoliday.Add( "0701" , "建党节" ); gHoliday.Add( "0801" , "建军节" ); gHoliday.Add( "0910" , "教师节" ); gHoliday.Add( "1001" , "国庆节" ); gHoliday.Add( "1224" , "平安夜" ); gHoliday.Add( "1225" , "圣诞节" ); //农历节日 nHoliday.Add( "0101" , "春节" ); nHoliday.Add( "0115" , "元宵节" ); nHoliday.Add( "0505" , "端午节" ); nHoliday.Add( "0815" , "中秋节" ); nHoliday.Add( "0909" , "重阳节" ); nHoliday.Add( "1208" , "腊八节" ); } /// <summary> /// 获取农历 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetChinaDate(DateTime dt) { if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime) { //日期范围:1901 年 2 月 19 日 - 2101 年 1 月 28 日 throw new Exception( string .Format( "日期超出范围!必须在{0}到{1}之间!" , china.MinSupportedDateTime.ToString( "yyyy-MM-dd" ), china.MaxSupportedDateTime.ToString( "yyyy-MM-dd" ))); } string str = string .Format( "{0} {1}{2}" , GetYear(dt), GetMonth(dt), GetDay(dt)); string strJQ = GetSolarTerm(dt); if (strJQ != "" ) { str = " (" strJQ ")" ; } string strHoliday = GetHoliday(dt); if (strHoliday != "" ) { str = " " strHoliday; } string strChinaHoliday = GetChinaHoliday(dt); if (strChinaHoliday != "" ) { str = " " strChinaHoliday; } return str; } /// <summary> /// 获取农历年份 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetYear(DateTime dt) { int yearIndex = china.GetSexagenaryYear(dt); string yearTG = " 甲乙丙丁戊己庚辛壬癸" ; string yearDZ = " 子丑寅卯辰巳午未申酉戌亥" ; string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪" ; int year = china.GetYear(dt); int yTG = china.GetCelestialStem(yearIndex); int yDZ = china.GetTerrestrialBranch(yearIndex); string str = string .Format( "[{1}]{2}{3}{0}" , year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]); return str; } /// <summary> /// 获取农历月份 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetMonth(DateTime dt) { int year = china.GetYear(dt); int iMonth = china.GetMonth(dt); int leapMonth = china.GetLeapMonth(year); bool isLeapMonth = iMonth == leapMonth; if (leapMonth != 0 && iMonth >= leapMonth) { iMonth--; } string szText = "正二三四五六七八九十" ; string strMonth = isLeapMonth ? "闰" : "" ; if (iMonth <= 10) { strMonth = szText.Substring(iMonth - 1, 1); } else if (iMonth == 11) { strMonth = "十一" ; } else { strMonth = "腊" ; } return strMonth "月" ; } /// <summary> /// 获取农历日期 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetDay(DateTime dt) { int iDay = china.GetDayOfMonth(dt); string szText1 = "初十廿三" ; string szText2 = "一二三四五六七八九十" ; string strDay; if (iDay == 20) { strDay = "二十" ; } else if (iDay == 30) { strDay = "三十" ; } else { strDay = szText1.Substring((iDay - 1) / 10, 1); strDay = strDay szText2.Substring((iDay - 1) % 10, 1); } return strDay; } /// <summary> /// 获取节气 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetSolarTerm(DateTime dt) { DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0); DateTime dtNew; double num; int y; string strReturn = "" ; y = dt.Year; for ( int i = 1; i <= 24; i ) { num = 525948.76 * (y - 1900) JQData[i - 1]; dtNew = dtBase.AddMinutes(num); if (dtNew.DayOfYear == dt.DayOfYear) { strReturn = JQ[i - 1]; } } return strReturn; } /// <summary> /// 获取公历节日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetHoliday(DateTime dt) { string strReturn = "" ; object g = gHoliday[dt.Month.ToString( "00" ) dt.Day.ToString( "00" )]; if (g != null ) { strReturn = g.ToString(); } return strReturn; } /// <summary> /// 获取农历节日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetChinaHoliday(DateTime dt) { string strReturn = "" ; int year = china.GetYear(dt); int iMonth = china.GetMonth(dt); int leapMonth = china.GetLeapMonth(year); int iDay = china.GetDayOfMonth(dt); if (china.GetDayOfYear(dt) == china.GetDaysInYear(year)) { strReturn = "除夕" ; } else if (leapMonth != iMonth) { if (leapMonth != 0 && iMonth >= leapMonth) { iMonth--; } object n = nHoliday[iMonth.ToString( "00" ) iDay.ToString( "00" )]; if (n != null ) { if (strReturn == "" ) { strReturn = n.ToString(); } else { strReturn = " " n.ToString(); } } } return strReturn; } } |
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论