📄 monthcalendar.cs
字号:
get
{
return this.week;
}
set
{
this.week = value;
}
}
private int day = 0;
/// <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;
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="month"></param>
/// <param name="week"></param>
/// <param name="day"></param>
/// <param name="name"></param>
public WeekDayFestival(int month, int week, int day, string name)
{
this.Month = month;
this.Week = week;
this.Day = day;
this.Name = name;
}
}
/// <summary>
/// 日历元素
/// </summary>
public class CalendarElement
{
/// <summary>
/// 构造函数
/// </summary>
public CalendarElement()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="solarDate"></param>
/// <param name="lunarDate"></param>
public CalendarElement(DateTime solarDate, LunarDate lunarDate)
{
this.SolarDate = solarDate;
this.LunarDate = lunarDate;
}
private DateTime solarDate = DateTime.Now;
/// <summary>
/// 公历日期
/// </summary>
public DateTime SolarDate
{
get
{
return this.solarDate;
}
set
{
this.solarDate = value;
}
}
private LunarDate lunarDate = MonthCalendar.GetLunarDate(DateTime.Now);
/// <summary>
/// 农历日期
/// </summary>
public LunarDate LunarDate
{
get
{
return this.lunarDate;
}
set
{
this.lunarDate = value;
}
}
private Festival solarFestival = null;
/// <summary>
/// 公历节日
/// </summary>
public Festival SolarFestival
{
get
{
return this.solarFestival;
}
set
{
this.solarFestival = value;
}
}
private Festival lunarFestival = null;
/// <summary>
/// 农历节日
/// </summary>
public Festival LunarFestival
{
get
{
return this.lunarFestival;
}
set
{
this.lunarFestival = value;
}
}
private WeekDayFestival weekDaySolarFestival = null;
/// <summary>
/// 基于周/日的节日
/// </summary>
public WeekDayFestival WeekDaySolarFestival
{
get
{
return this.weekDaySolarFestival;
}
set
{
this.weekDaySolarFestival = value;
}
}
private string solarTerms = null;
/// <summary>
/// 节气
/// </summary>
public string SolarTerms
{
get
{
if(this.solarTerms == null)
this.solarTerms = "";
return this.solarTerms;
}
set
{
this.solarTerms = value;
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(this.SolarDate.ToString(
"yyyy年MM月dd日 dddd"));
sb.Append(" 农历: " + this.LunarDate.ToString());
if(this.SolarFestival != null)
sb.Append("\n公历节日: " + this.SolarFestival.Name);
if(this.WeekDaySolarFestival != null)
sb.Append("\n特殊公历节日: " + this.WeekDaySolarFestival.Name);
if(this.LunarFestival != null)
sb.Append("\n农历节日: " + this.LunarFestival.Name);
if(this.SolarTerms != null && this.SolarTerms != "")
sb.Append("\n节气: " + this.SolarTerms);
return sb.ToString();
}
}
/// <summary>
/// 农历日期
/// </summary>
public class LunarDate
{
private int year = 0;
public int Year
{
get
{
return this.year;
}
set
{
this.year = value;
}
}
private bool isLeap = false;
public bool IsLeap
{
get
{
return this.isLeap;
}
set
{
this.isLeap = value;
}
}
private int month = 0;
public int Month
{
get
{
return this.month;
}
set
{
this.month = value;
}
}
private int day = 0;
public int Day
{
get
{
return this.day;
}
set
{
this.day = value;
}
}
/// <summary>
/// 构造函数
/// </summary>
public LunarDate()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <param name="isLeap"></param>
public LunarDate(int year, int month, int day, bool isLeap)
{
this.Year = year;
this.Month = month;
this.Day = day;
this.IsLeap = isLeap;
}
public override string ToString()
{
return this.Year + "年" + this.Month + "月" + this.Day + "日";
}
}
private int length;
public int Length
{
get
{
return this.length;
}
set
{
this.length = value;
}
}
private int firstWeek = 0;
public int FirstWeek
{
get
{
return this.firstWeek;
}
set
{
this.firstWeek = value;
}
}
private Hashtable elements = new Hashtable();
public CalendarElement this[int index]
{
get
{
if(this.elements.ContainsKey(index))
return this.elements[index] as CalendarElement;
return null;
}
set
{
this.elements[index] = value;
}
}
/// <summary>
/// 构造函数
/// </summary>
public MonthCalendar()
:this(DateTime.Now.Year, DateTime.Now.Month)
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="solarYear"></param>
/// <param name="solarMonth"></param>
public MonthCalendar(int solarYear, int solarMonth)
{
this.SetMonth(solarYear, solarMonth);
}
/// <summary>
/// 设置当前实例的年和月
/// </summary>
/// <param name="solarYear"></param>
/// <param name="solarMonth"></param>
public void SetMonth(int solarYear,int solarMonth)
{
DateTime sDObj;
LunarDate lDObj;
int lY = 0, lM = 0;
int lD=1;
bool lL = false;
int lX=0, tmp1 = 0, tmp2 = 0, tmp3 = 0;
int[] lDPOS = new int[4]{0, 0, 0, 0};
int n = 0;
int firstLM = 0;
sDObj = new DateTime(solarYear,solarMonth,1); //当月一日日期
this.Length = GetSolarDays(solarYear,solarMonth); //公历当月天数
this.FirstWeek = sDObj.Day; //公历当月1日星期几
for(int i=0;i< this.Length;i++)
{
if(lD>lX)
{
sDObj = new DateTime(solarYear,solarMonth,i+1); //当月一日日期
lDObj = MonthCalendar.GetLunarDate(sDObj); //农历
lY = lDObj.Year; //农历年
lM = lDObj.Month; //农历月
lD = lDObj.Day; //农历日
lL = lDObj.IsLeap; //农历是否闰月
lX = lL? GetLeapDays(lY): GetLunarDays(lY,lM); //农历当月最后一天
if(n==0) firstLM = lM;
lDPOS[++n] = i-lD+1;
}
DateTime solarDate = new DateTime(solarYear, solarMonth, i + 1);
LunarDate lunarDate = new LunarDate(lY, lM, lD++, lL);
this[i] = new CalendarElement(solarDate,lunarDate);
}
//节气
tmp1=GetSolarTerm(solarYear,(solarMonth - 1) * 2 );
tmp2=GetSolarTerm(solarYear,(solarMonth - 1) * 2 +1 );
this[tmp1 - 1].SolarTerms = solarTerm[(solarMonth - 1)*2];
this[tmp2 - 1].SolarTerms = solarTerm[(solarMonth - 1)*2+1];
//公历节日
foreach(Festival fest in MonthCalendar.solarFestivals)
{
if(fest.Month == (solarMonth))
{
this[fest.Day - 1].SolarFestival = fest;
}
}
//月周节日
foreach(WeekDayFestival fest in MonthCalendar.weekDayFestivals)
{
if(fest.Month == (solarMonth))
{
tmp1 = fest.Week;
tmp2 = fest.Day;
if(tmp1<5)
this[((this.FirstWeek>tmp2)?7:0) + 7*(tmp1-1) + tmp2 - this.FirstWeek].WeekDaySolarFestival = fest;
else
{
tmp1 -= 5;
tmp3 = (this.FirstWeek+this.Length-1)%7; //当月最后一天星期?
this[this.Length - tmp3 - 7*tmp1 + tmp2 - (tmp2>tmp3?7:0) - 1 ].WeekDaySolarFestival = fest;
}
}
}
//农历节日
foreach(Festival fest in MonthCalendar.lunarFestivals)
{
// tmp1=fest.Month - firstLM;
// if(tmp1==-11) tmp1=1;
// if(tmp1 >=0 && tmp1<n)
// {
// tmp2 = lDPOS[tmp1] + fest.Day -1;
// if(tmp2 >= 0 && tmp2<this.Length && this[tmp2].LunarDate.IsLeap!=true)
// {
// this[tmp2].LunarFestival = fest;
// }
// }
foreach(CalendarElement elem in this.elements.Values)
{
if(elem.LunarDate.Month == fest.Month
&& elem.LunarDate.Day == fest.Day)
elem.LunarFestival = fest;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -