实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | /// <summary> /// 中国日历信息实体类 /// cncxz(虫虫) 2007-2-9 /// </summary> public sealed class ChineseCalendarInfo { private DateTime m_SolarDate; private int m_LunarYear, m_LunarMonth, m_LunarDay; private bool m_IsLeapMonth = false ; private string m_LunarYearSexagenary = null , m_LunarYearAnimal = null ; private string m_LunarYearText = null , m_LunarMonthText = null , m_LunarDayText = null ; private string m_SolarWeekText = null , m_SolarConstellation = null , m_SolarBirthStone = null ; #region 构造函数 public ChineseCalendarInfo() : this (DateTime.Now.Date) { } /// <summary> /// 从指定的阳历日期创建中国日历信息实体类 /// </summary> /// <param name="date">指定的阳历日期</param> public ChineseCalendarInfo(DateTime date) { m_SolarDate = date; LoadFromSolarDate(); } private void LoadFromSolarDate() { 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); int leapMonth = calendar.GetLeapMonth(m_LunarYear); if (leapMonth == m_LunarMonth) { m_IsLeapMonth = true ; m_LunarMonth -= 1; } else if (leapMonth > 0 && leapMonth < m_LunarMonth) { m_LunarMonth -= 1; } m_LunarDay = calendar.GetDayOfMonth(m_SolarDate); CalcConstellation(m_SolarDate, out m_SolarConstellation, out m_SolarBirthStone); } #endregion #region 日历属性 /// <summary> /// 阳历日期 /// </summary> public DateTime SolarDate { get { return m_SolarDate; } set { if (m_SolarDate.Equals(value)) return ; m_SolarDate = value; LoadFromSolarDate(); } } /// <summary> /// 星期几 /// </summary> public string SolarWeekText { get { if ( string .IsNullOrEmpty(m_SolarWeekText)) { int i = ( int ) m_SolarDate.DayOfWeek; m_SolarWeekText = ChineseWeekName[i]; } return m_SolarWeekText; } } /// <summary> /// 阳历星座 /// </summary> public string SolarConstellation { get { return m_SolarConstellation; } } /// <summary> /// 阳历诞生石 /// </summary> public string SolarBirthStone { get { return m_SolarBirthStone; } } /// <summary> /// 阴历年份 /// </summary> public int LunarYear { get { return m_LunarYear; } } /// <summary> /// 阴历月份 /// </summary> public int LunarMonth { get { return m_LunarMonth; } } /// <summary> /// 是否阴历闰月 /// </summary> public bool IsLeapMonth { get { return m_IsLeapMonth; } } /// <summary> /// 阴历月中日期 /// </summary> public int LunarDay { get { return m_LunarDay; } } /// <summary> /// 阴历年干支 /// </summary> public string LunarYearSexagenary { get { if ( string .IsNullOrEmpty(m_LunarYearSexagenary)) { int y = calendar.GetSexagenaryYear( this .SolarDate); m_LunarYearSexagenary = CelestialStem.Substring((y - 1) % 10, 1) TerrestrialBranch.Substring((y - 1) % 12, 1); } return m_LunarYearSexagenary; } } /// <summary> /// 阴历年生肖 /// </summary> public string LunarYearAnimal { get { if ( string .IsNullOrEmpty(m_LunarYearAnimal)) { int y = calendar.GetSexagenaryYear( this .SolarDate); m_LunarYearAnimal = Animals.Substring((y - 1) % 12, 1); } return m_LunarYearAnimal; } } /// <summary> /// 阴历年文本 /// </summary> public string LunarYearText { get { if ( string .IsNullOrEmpty(m_LunarYearText)) { m_LunarYearText = Animals.Substring(calendar.GetSexagenaryYear( new DateTime(m_LunarYear, 1, 1)) % 12 - 1, 1); StringBuilder sb = new StringBuilder(); int year = this .LunarYear; int d; do { d = year % 10; sb.Insert(0, ChineseNumber[d]); year = year / 10; } while (year > 0); m_LunarYearText = sb.ToString(); } return m_LunarYearText; } } /// <summary> /// 阴历月文本 /// </summary> public string LunarMonthText { get { if ( string .IsNullOrEmpty(m_LunarMonthText)) { m_LunarMonthText = ( this .IsLeapMonth ? "闰" : "" ) ChineseMonthName[ this .LunarMonth - 1]; } return m_LunarMonthText; } } /// <summary> /// 阴历月中日期文本 /// </summary> public string LunarDayText { get { if ( string .IsNullOrEmpty(m_LunarDayText)) m_LunarDayText = ChineseDayName[ this .LunarDay - 1]; return m_LunarDayText; } } #endregion /// <summary> /// 根据指定阳历日期计算星座&诞生石 /// </summary> /// <param name="date">指定阳历日期</param> /// <param name="constellation">星座</param> /// <param name="birthstone">诞生石</param> public static void CalcConstellation(DateTime date, out string constellation, out string birthstone) { int i = Convert.ToInt32(date.ToString( "MMdd" )); int j; if (i >= 321 && i <= 419) j = 0; else if (i >= 420 && i <= 520) j = 1; else if (i >= 521 && i <= 621) j = 2; else if (i >= 622 && i <= 722) j = 3; else if (i >= 723 && i <= 822) j = 4; else if (i >= 823 && i <= 922) j = 5; else if (i >= 923 && i <= 1023) j = 6; else if (i >= 1024 && i <= 1121) j = 7; else if (i >= 1122 && i <= 1221) j = 8; else if (i >= 1222 || i <= 119) j = 9; else if (i >= 120 && i <= 218) j = 10; else if (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> /// <param name="year">指定的年份</param> private static DateTime GetLunarNewYearDate( int year) { DateTime dt = new DateTime(year, 1, 1); int cnYear = calendar.GetYear(dt); int cnMonth = calendar.GetMonth(dt); int num1 = 0; int num2 = calendar.IsLeapYear(cnYear) ? 13 : 12; while (num2 >= cnMonth) { num1 = calendar.GetDaysInMonth(cnYear, num2--); } num1 = num1 - calendar.GetDayOfMonth(dt) 1; return dt.AddDays(num1); } /// <summary> /// 阴历转阳历 /// </summary> /// <param name="year">阴历年</param> /// <param name="month">阴历月</param> /// <param name="day">阴历日</param> /// <param name="IsLeapMonth">是否闰月</param> public static DateTime GetDateFromLunarDate( int year, int month, int day, bool IsLeapMonth) { if (year < 1902 || year > 2100) throw new Exception( "只支持1902~2100期间的农历年" ); if (month < 1 || month > 12) throw new Exception( "表示月份的数字必须在1~12之间" ); if (day < 1 || day > calendar.GetDaysInMonth(year, month)) throw new Exception( "农历日期输入有误" ); int num1 = 0, num2 = 0; int leapMonth = 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--); } DateTime dt = GetLunarNewYearDate(year); return dt.AddDays(num1 day - 1); } /// <summary> /// 阴历转阳历 /// </summary> /// <param name="date">阴历日期</param> /// <param name="IsLeapMonth">是否闰月</param> public static DateTime GetDateFromLunarDate(DateTime date, bool IsLeapMonth) { return GetDateFromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth); } #endregion #region 从阴历创建日历 /// <summary> /// 从阴历创建日历实体 /// </summary> /// <param name="year">阴历年</param> /// <param name="month">阴历月</param> /// <param name="day">阴历日</param> /// <param name="IsLeapMonth">是否闰月</param> public static ChineseCalendarInfo FromLunarDate( int year, int month, int day, bool IsLeapMonth) { DateTime dt = GetDateFromLunarDate(year, month, day, IsLeapMonth); return new ChineseCalendarInfo(dt); } /// <summary> /// 从阴历创建日历实体 /// </summary> /// <param name="date">阴历日期</param> /// <param name="IsLeapMonth">是否闰月</param> public static ChineseCalendarInfo FromLunarDate(DateTime date, bool IsLeapMonth) { return FromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth); } /// <summary> /// 从阴历创建日历实体 /// </summary> /// <param name="date">表示阴历日期的8位数字,例如:20070209</param> /// <param name="IsLeapMonth">是否闰月</param> public static ChineseCalendarInfo FromLunarDate( string date, bool IsLeapMonth) { Regex rg = new System.Text.RegularExpressions.Regex( @"^/d{7}(/d)$" ); Match mc = rg.Match(date); if (!mc.Success) { throw new Exception( "日期字符串输入有误!" ); } DateTime dt = DateTime.Parse( string .Format( "{0}-{1}-{2}" , date.Substring(0, 4), date.Substring(4, 2), date.Substring(6, 2))); return FromLunarDate(dt, IsLeapMonth); } #endregion private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar(); public const string ChineseNumber = "〇一二三四五六七八九" ; public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸" ; public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥" ; public const string Animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪" ; public static readonly string [] ChineseWeekName = new string [] { "星期天" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" }; public static readonly string [] ChineseDayName = new string [] { "初一" , "初二" , "初三" , "初四" , "初五" , "初六" , "初七" , "初八" , "初九" , "初十" , "十一" , "十二" , "十三" , "十四" , "十五" , "十六" , "十七" , "十八" , "十九" , "二十" , "廿一" , "廿二" , "廿三" , "廿四" , "廿五" , "廿六" , "廿七" , "廿八" , "廿九" , "三十" }; public static readonly string [] ChineseMonthName = new string [] { "正" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九" , "十" , "十一" , "十二" }; public static readonly string [] Constellations = new string [] { "白羊座" , "金牛座" , "双子座" , "巨蟹座" , "狮子座" , "处女座" , "天秤座" , "天蝎座" , "射手座" , "摩羯座" , "水瓶座" , "双鱼座" }; public static readonly string [] BirthStones = new string [] { "钻石" , "蓝宝石" , "玛瑙" , "珍珠" , "红宝石" , "红条纹玛瑙" , "蓝宝石" , "猫眼石" , "黄宝石" , "土耳其玉" , "紫水晶" , "月长石,血石" }; } |
好例子网口号:伸出你的我的手 — 分享!
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)