⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monthcalendar.cs

📁 该源代码用 C# 写成
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.Text;

namespace Org.InteliIM.Activities.Schedule
{
	/// <summary>
	/// 带农历的日历
	/// </summary>
	public class MonthCalendar
	{
		#region 静态成员

		private static int[] lunarInfo=new int[]{
											 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
											 0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
											 0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
											 0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
											 0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
											 0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
											 0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
											 0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
											 0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
											 0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
											 0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
											 0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
											 0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
											 0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
											 0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
											 0x14b63};

		private static  int[] solarMonth=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
		private static  string[] Animals = new string[]{"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
		private static  string[] solarTerm = new string[]{"小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至"};
		private static  int[] solarTermInfo = new int[]{0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758};
		private static  string[] nStr1 = new string[]{"日","一","二","三","四","五","六","七","八","九","十"};
		private static  string[] nStr2 = new string[]{"初","十","廿","卅","□"};

		/// <summary>
		/// 国历节日
		/// </summary>
		private static Festival[] solarFestivals = new Festival[]{
																	 new Festival(1, 1, "元旦节", true),
																	 new Festival(2, 2, "世界湿地日"), 
																	 new Festival(2, 10, "国际气象节"), 
																	 new Festival(2, 14, "情人节"), 
																	 new Festival(3, 1, "国际海豹日"), 
																	 new Festival(3, 3, "全国爱耳日"), 
																	 new Festival(3, 5, "学雷锋纪念日"), 
																	 new Festival(3, 8, "妇女节"), 
																	 new Festival(3, 12, "植树节 孙中山逝世纪念日"),
																	 new Festival(3, 14, "国际警察日"), 
																	 new Festival(3, 15, "消费者权益日"), 
																	 new Festival(3, 17, "中国国医节 国际航海日"), 
																	 new Festival(3, 21, "世界森林日 消除种族歧视国际日 世界儿歌日"), 
																	 new Festival(3, 22, "世界水日"), 
																	 new Festival(3, 23, "世界气象日"), 
																	 new Festival(3, 24, "世界防治结核病日"), 
																	 new Festival(3, 25, "全国中小学生安全教育日"), 
																	 new Festival(3, 30, "巴勒斯坦国土日"), 
																	 new Festival(4, 1, "愚人节 全国爱国卫生运动月(四月) 税收宣传月(四月)"), 
																	 new Festival(4,7,"世界卫生日"), 
																	 new Festival(4,22,"世界地球日"), 
																	 new Festival(4,23,"世界图书和版权日"), 
																	 new Festival(4,24,"亚非新闻工作者日"), 
																	 new Festival(5,1,"劳动节", true), 
																	 new Festival(5,2,"劳动节假日",true), 
																	 new Festival(5,3,"劳动节假日", true), 
																	 new Festival(5,4,"青年节"), 
																	 new Festival(5,5,"碘缺乏病防治日"), 
																	 new Festival(5,8,"世界红十字日"), 
																	 new Festival(5,12,"国际护士节"), 
																	 new Festival(5,15,"国际家庭日"), 
																	 new Festival(5,17,"国际电信日"), 
																	 new Festival(5,18,"国际博物馆日"), 
																	 new Festival(5,20,"全国学生营养日"), 
																	 new Festival(5,23,"国际牛奶日"), 
																	 new Festival(5,31,"世界无烟日"), 
																	 new Festival(6, 1, "国际儿童节"), 
																	 new Festival(6, 5, "世界环境保护日"), 
																	 new Festival(6, 6, "全国爱眼日"), 
																	 new Festival(6, 17, "防治荒漠化和干旱日"), 
																	 new Festival(6, 23, "国际奥林匹克日"), 
																	 new Festival(6, 25, "全国土地日"), 
																	 new Festival(6, 26, "国际禁毒日"), 
																	 new Festival(7, 1, "香港回归纪念日 中共诞辰 世界建筑日"), 
																	 new Festival(7, 2, "国际体育记者日"), 
																	 new Festival(7, 7, "抗日战争纪念日"), 
																	 new Festival(7, 11, "世界人口日"), 
																	 new Festival(7, 30, "非洲妇女日"), 
																	 new Festival(8, 1, "建军节"),
																	 new Festival(8, 8, "中国男子节(爸爸节)"), 
																	 new Festival(8, 15, "抗日战争胜利纪念"),
																	 new Festival(9, 8, "国际扫盲日 国际新闻工作者日"),
																	 new Festival(9, 9, "毛泽东逝世纪念"),
																	 new Festival(9, 10, "中国教师节"),
																	 new Festival(9, 14, "世界清洁地球日"),
																	 new Festival(9, 16, "国际臭氧层保护日"),
																	 new Festival(9, 18, "九·一八事变纪念日"), 
																	 new Festival(9, 20, "国际爱牙日"),
																	 new Festival(9, 27, "世界旅游日"),
																	 new Festival(9, 28, "孔子诞辰"), 
																	 new Festival(10, 1, "国庆节 世界音乐日 国际老人节", true),
																	 new Festival(10, 2, "国庆节假日 国际和平与民主自由斗争日", true),
																	 new Festival(10, 3, "国庆节假日", true),
																	 new Festival(10, 4, "世界动物日"),
																	 new Festival(10, 6, "老人节"),
																	 new Festival(10, 8, "全国高血压日 世界视觉日"),
																	 new Festival(10, 9, "世界邮政日 万国邮联日"),
																	 new Festival(10, 10, "辛亥革命纪念日 世界精神卫生日"),
																	 new Festival(10, 13, "世界保健日 国际教师节"),
																	 new Festival(10, 14, "世界标准日"),
																	 new Festival(10, 15, "国际盲人节(白手杖节)"),
																	 new Festival(10, 16, "世界粮食日"),
																	 new Festival(10, 17, "世界消除贫困日"),
																	 new Festival(10, 22, "世界传统医药日"),
																	 new Festival(10, 24, "联合国日"), 
																	 new Festival(10, 31, "世界勤俭日"),
																	 new Festival(11, 7, "十月社会主义革命纪念日"),
																	 new Festival(11, 8, "中国记者日"),
																	 new Festival(11, 9, "全国消防安全宣传教育日"),
																	 new Festival(11, 10, "世界青年节"),
																	 new Festival(11, 11, "国际科学与和平周(本日所属的一周)"),
																	 new Festival(11, 12, "孙中山诞辰纪念日"),
																	 new Festival(11, 14, "世界糖尿病日"),
																	 new Festival(11, 17, "国际大学生节 世界学生节"),
																	 new Festival(11, 20, "彝族年", true),
																	 new Festival(11, 21, "彝族年 世界问候日 世界电视日", true),
																	 new Festival(11, 22, "彝族年", true),
																	 new Festival(11, 29, "国际声援巴勒斯坦人民国际日"),
																	 new Festival(12, 1, "世界艾滋病日"),
																	 new Festival(12, 3, "世界残疾人日"),
																	 new Festival(12, 5, "国际经济和社会发展志愿人员日"),
																	 new Festival(12, 8, "国际儿童电视日"),
																	 new Festival(12, 9, "世界足球日"),
																	 new Festival(12, 10, "世界人权日"),
																	 new Festival(12, 12, "西安事变纪念日"),
																	 new Festival(12, 13, "南京大屠杀(1937年)纪念日!紧记血泪史!"),
																	 new Festival(12, 20, "澳门回归纪念"), 
																	 new Festival(12, 21, "国际篮球日"),
																	 new Festival(12, 24, "平安夜"),
																	 new Festival(12, 25, "圣诞节"),
																	 new Festival(12, 26, "毛泽东诞辰纪念")};

		/// <summary>
		/// 农历节日
		/// </summary>
		private static Festival[] lunarFestivals = new Festival[]{
																	 new Festival(1, 1, "春节", true),
																	 new Festival(1, 2, "春节", true),
																	 new Festival(1, 3, "春节", true),
																	 new Festival(1, 15, "元宵节"),
																	 new Festival(5, 5, "端午节"),
																	 new Festival(6, 24, "火把节", true), 
																	 new Festival(6, 25, "火把节", true),
																	 new Festival(6, 26, "火把节", true), 
																	 new Festival(7, 7, "七夕情人节"), 
																	 new Festival(7, 15, "中元节"),
																	 new Festival(8, 15, "中秋节"),
																	 new Festival(9, 9, "重阳节"),
																	 new Festival(12, 8, "腊八节"), 
																	 new Festival(12, 24, "小年"),
																	 new Festival(1, 0, "除夕")};

		/// <summary>
		/// 某月的第几个星期几
		/// </summary>
		private static WeekDayFestival[] weekDayFestivals = new WeekDayFestival[]{
																					 new WeekDayFestival(1, 5, 0, "世界麻风日"), //一月的最后一个星期日(月倒数第一个星期日)
																					 new WeekDayFestival(5, 2,0, "国际母亲节"),
																					 new WeekDayFestival(5, 3, 0, "全国助残日"),
																					 new WeekDayFestival(6, 3, 0, "父亲节"),
																					 new WeekDayFestival(7, 3, 0, "被奴役国家周"),
																					 new WeekDayFestival(9, 3, 2, "国际和平日"),
																					 new WeekDayFestival(9, 4, 0, "国际聋人节 世界儿童日"),
																					 new WeekDayFestival(9, 5, 0, "世界海事日"),
																					 new WeekDayFestival(10, 1, 1, "国际住房日"),
																					 new WeekDayFestival(10, 1, 3, "国际减轻自然灾害日(减灾日)"),
																					 new WeekDayFestival(11, 4, 4, "感恩节")};

		/// <summary>
		/// 返回农历 y 年的总天数
		/// </summary>
		/// <param name="lunarYear"></param>
		/// <returns></returns>
		public static int GetLunarDays(int lunarYear) 
		{
			int i, sum = 348;
			for(i=0x8000; i>0x8; i>>=1) sum += ((lunarInfo[lunarYear-1900] & i) != 0)? 1: 0;
			return(sum+GetLeapDays(lunarYear));
		}

		/// <summary>
		/// 返回农历 y 年闰月的天数
		/// </summary>
		/// <param name="year"></param>
		/// <returns></returns>
		public static int GetLeapDays(int year) 
		{
			if(GetLeapMonth(year) != 0)  return(((lunarInfo[year-1900] & 0x10000) != 0)? 30: 29);
			else return(0);
		}

		/// <summary>
		/// 返回农历 y年闰哪个月 1-12 , 没闰返回 0
		/// </summary>
		/// <param name="year"></param>
		/// <returns></returns>
		public static int GetLeapMonth(int year) 
		{
			return(lunarInfo[year-1900] & 0xf);
		}

		/// <summary>
		/// 返回公历某年某月的天数
		/// </summary>
		/// <param name="year">年</param>
		/// <param name="month">月</param>
		/// <returns>该月的天数</returns>
		public static int GetSolarDays(int year,int month) 
		{
			if(month==2)
				return(((year%4 == 0) && (year%100 != 0) || (year%400 == 0))? 29: 28);
			else
				return(solarMonth[month - 1]);
		}

		/// <summary>
		/// 返回农历 y年m月的总天数
		/// </summary>
		/// <param name="lunarYear"></param>
		/// <param name="lunarMonth"></param>
		/// <returns></returns>
		public static int GetLunarDays(int lunarYear, int lunarMonth) 
		{
			return( ((lunarInfo[lunarYear-1900] & (0x10000>>lunarMonth)) != 0)? 30: 29 );
		}

		/// <summary>
		/// 返回公历日期对应的农历日期
		/// </summary>
		/// <param name="solarDate">公历日期</param>
		/// <returns>农历日期</returns>
		public static LunarDate GetLunarDate(DateTime solarDate) 
		{
			LunarDate lunarDate = new LunarDate();
			int i, leap=0, temp=0;

			int offset = solarDate.Subtract(new DateTime(1900, 1, 31)).Days;

			for(i=1900; i<2050 && offset>0; i++) { temp=GetLunarDays(i); offset-=temp; }

			if(offset<0) { offset+=temp; i--; }

			lunarDate.Year = i;

			leap = GetLeapMonth(i); //闰哪个月
			lunarDate.IsLeap = false;

			for(i=1; i<13 && offset>0; i++) 
			{
				//闰月
				if(leap>0 && i==(leap+1) && lunarDate.IsLeap==false)
				{ --i; lunarDate.IsLeap = true; temp = GetLeapDays(lunarDate.Year); }
				else
				{ temp = GetLunarDays(lunarDate.Year, i); }

				//解除闰月
				if(lunarDate.IsLeap==true && i==(leap+1)) lunarDate.IsLeap = false;

				offset -= temp;
			}

			if(offset==0 && leap>0 && i==leap+1)
				if(lunarDate.IsLeap)
				{ lunarDate.IsLeap = false; }
				else
				{ lunarDate.IsLeap = true; --i; }

			if(offset<0){ offset += temp; --i; }

			lunarDate.Month = i;
			lunarDate.Day = offset + 1;

			return lunarDate;
		}

		/// <summary>
		/// 返回某年的第n个节气为几日(从0小寒起算)
		/// </summary>
		/// <param name="year"></param>
		/// <param name="no"></param>
		/// <returns></returns>
		public static int GetSolarTerm(int year,int no) 
		{
			DateTime originDate = new DateTime(1900, 1, 6, 2, 5, 0);

			DateTime offDate = originDate.AddMilliseconds(31556925974.7*(year-1900) + (double)MonthCalendar.solarTermInfo[no]*60000);

			return(offDate.Day);
		}

		/// <summary>
		/// 返回某年的生肖
		/// </summary>
		/// <param name="solarYear"></param>
		/// <returns></returns>
		public static string GetAnimal(int solarYear)
		{
			return MonthCalendar.Animals[(solarYear-4)%12];
		}

		/// <summary>
		/// 获取农历日期的中文名称
		/// </summary>
		/// <param name="lunarDay"></param>
		/// <returns></returns>
		public static string GetLunarDayName(int lunarDay)
		{
			string s = "";

			switch (lunarDay) 
			{
				case 10:
					s = "初十";
					break;
				case 20:
					s = "二十"; 
					break;
				case 30:
					s = "三十"; 
					break;
				default :
					s = nStr2[(int) Math.Floor(lunarDay/10)] + nStr1[lunarDay%10];
					break;
			}
			return(s);
		}

		#endregion

		/// <summary>
		/// 节日
		/// </summary>
		public class Festival
		{
			private int month = 1;

			/// <summary>
			/// 月
			/// </summary>
			public int Month
			{
				get
				{
					return this.month;
				}
				set
				{
					this.month = value;
				}
			}

			private int day = 1;

			/// <summary>
			/// 日
			/// </summary>
			public int Day
			{
				get
				{
					return this.day;
				}
				set
				{
					this.day = value;
				}
			}

			private string name = null;

			/// <summary>
			/// 节日名称
			/// </summary>
			public string Name
			{
				get
				{
					if(this.name == null)
						this.name = "";

					return this.name;
				}
				set
				{
					this.name = value;
				}
			}

			private bool vocation = false;

			/// <summary>
			/// 是否放假
			/// </summary>
			public bool Vocation
			{
				get
				{
					return this.vocation;
				}
				set
				{
					this.vocation = value;	
				}
			}

			/// <summary>
			/// 构造函数
			/// </summary>
			/// <param name="month"></param>
			/// <param name="day"></param>
			/// <param name="name"></param>
			public Festival(int month, int day, string name)
				:this(month, day, name, false)
			{
			}

			/// <summary>
			/// 构造函数
			/// </summary>
			/// <param name="month"></param>
			/// <param name="day"></param>
			/// <param name="name"></param>
			/// <param name="vocation"></param>
			public Festival(int month, int day, string name, bool vocation)
			{
				this.Month = month;
				this.Day = day;
				this.Name = name;
				this.Vocation = vocation;
			}
		}

		/// <summary>
		/// 基于某月的第几个星期几的节日
		/// </summary>
		public class WeekDayFestival
		{
			private int month = 1;

			/// <summary>
			/// 月
			/// </summary>
			public int Month
			{
				get
				{
					return this.month;
				}
				set
				{
					this.month = value;	
				}
			}

			private int week = 1;

			/// <summary>
			/// 周
			/// </summary>
			public int Week
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -