本文介绍了算法找到中国春节的公历日期一定阳历一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在驱动来计算在给定时间跨度各种假日。所以,我需要找到所有中国节日的公历日期(中国农历新年,清明节,端午节等)。我用了著名的复活节算法为耶稣受难日,复活节后的星期一,耶稣升天节,和圣灵降临节的计算;但是,我不明白它不够好,以适应其在中国历法。

我已经发现了类似的问题,但他们经常去从阳历到中国:

月/月相算法

Calculating探月/日月假期蟒蛇

http://www.herongyang.com/year/program.html

http://www.hermetic.ch/cal_stud/ch_year.htm

最后一个环节是exremely有益的,但​​我仍然不知道如何实现算法的方式,可以帮助我。任何意见或code会大大AP preciated!

下面是我的受难日算法:

 私人无效GetGoodFridayOccurances(日期时间的startDate,日期时间结束日期,名单,其中,ObservedHoliday> observedHolidays,StandardHoliday假期)
    {
        为(日期时间日期=的startDate;日期< =结束日期;日期= date.AddYears(1))
        {
            #地区寻找复活节算法的日
            INT日,月;
            INT firstTwo = date.Year / 100;
            INT remainderMod = date.Year%19;
            INT pfmDate =(firstTwo  -  15)/ 2 + 202  -  11 * remainderMod;
            #区域交换机
            开关(firstTwo)
            {
                案例21:
                案例24:
                案例25:
                案例27:
                案例28:
                案例29:
                案例30:
                案例31:
                案例32:
                案例34:
                案例35:
                案例38:
                    pfmDate = pfmDate  -  1;
                    打破;
                案例33:
                案例36:
                案例37:
                案例39:
                案例40:
                    pfmDate = pfmDate  -  2;
                    打破;
            }
            #endregion
            pfmDate = pfmDate%30;

            INT TA = pfmDate + 21;
            如果(pfmDate == 29)
                TA = TA  -  1;
            如果(pfmDate == 29&安培;&安培; remainderMod→10)
                TA = TA  -  1;
            //查找下周日
            INT TB =(TA  -  19)%7;

            INT TC =(40  -  firstTwo)%4;
            如果(TC == 3 ||的tC→1)
                TC = TC + 1;

            pfmDate = date.Year%100;
            INT tD型=(pfmDate + pfmDate / 4)%7;
            INT的TE = {((20  -  TB  - 达 -  TD)%7)+ 1;
            天= TA +的tE;

            如果(天> 31)
            {
                天=日 -  31日;
                一个月= 4;
            }
            其他
            {
                一个月= 3;
            }
            #endregion

            日期时间观察=新的日期时间(date.Year,月,日).AddDays(-2);
            ObservedHoliday obsdate =新ObservedHoliday(假日);
            如果(的startDate ==结束日期及放大器;&安培; startDate.Day == observed.Day)
            {
                obsdate.DateObserved =观察;
                observedHolidays.Add(obsdate);
            }
            否则,如果(的startDate =结束日期及放大器;!&安培;观察> =的startDate)
            {
                obsdate.DateObserved =观察;
                observedHolidays.Add(obsdate);
            }
        }
 

解决方案

有关中国农历新年,我想这会工作:

  ChineseLunisolarCalendar中国=新ChineseLunisolarCalendar();
GregorianCalendar的阳历=新的GregorianCalendar();

日期时间utcNow = DateTime.UtcNow;

//获取当前UTC日期/时间中国新年
日期时间chineseNewYear = chinese.ToDateTime(utcNow.Year,1,1,0,0,0,0);

//转换回公历(你可以只直接查询'的chineseNewYear`属性,但是我preFER使用`GregorianCalendar`的一致性:

INT32年= gregorian.GetYear(chineseNewYear);
INT32月= gregorian.GetMonth(chineseNewYear);
INT32天= gregorian.GetDayOfMonth(chineseNewYear);
 

I am making a driver to calculate various holidays in a given time span. So, I need to find the Gregorian dates of all the Chinese Holidays (Chinese New Year, QingMing Festival, Dragon Boat Festival, etc). I used the famous 'Easter algorithm' for Good Friday, Easter Monday, Ascension Day, and Whit Monday calculations; however, I don't understand it well enough to adapt it for the Chinese calendar.

I have found similar questions, but they often go from Gregorian to Chinese:

Moon / Lunar Phase Algorithm

Calculating lunar/lunisolar holidays in python

http://www.herongyang.com/year/program.html

http://www.hermetic.ch/cal_stud/ch_year.htm

The last link was exremely helpful but I'm still not sure how to implement that algorithm in a way that can help me. Any advice or code would be greatly appreciated!

Here is my Good Friday Algorithm:

private void GetGoodFridayOccurances(DateTime startDate, DateTime endDate,      List<ObservedHoliday> observedHolidays, StandardHoliday holiday)
    {
        for (DateTime date = startDate; date <= endDate; date = date.AddYears(1))
        {
            #region Finding the Day of Easter Algorithm
            int day, month;
            int firstTwo = date.Year / 100;
            int remainderMod = date.Year % 19;
            int pfmDate = (firstTwo - 15) / 2 + 202 - 11 * remainderMod;
            #region switches
            switch (firstTwo)
            {
                case 21:
                case 24:
                case 25:
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                case 32:
                case 34:
                case 35:
                case 38:
                    pfmDate = pfmDate - 1;
                    break;
                case 33:
                case 36:
                case 37:
                case 39:
                case 40:
                    pfmDate = pfmDate - 2;
                    break;
            }
            #endregion
            pfmDate = pfmDate % 30;

            int tA = pfmDate + 21;
            if (pfmDate == 29)
                tA = tA - 1;
            if (pfmDate == 29 && remainderMod > 10)
                tA = tA - 1;
            //Find next sunday
            int tB = (tA - 19) % 7;

            int tC = (40 - firstTwo) % 4;
            if (tC == 3 || tC > 1)
                tC = tC + 1;

            pfmDate = date.Year % 100;
            int tD = (pfmDate + pfmDate / 4) % 7;
            int tE = ((20 - tB - tC - tD) % 7) + 1;
            day = tA + tE;

            if (day > 31)
            {
                day = day - 31;
                month = 4;
            }
            else
            {
                month = 3;
            }
            #endregion

            DateTime observed = new DateTime(date.Year, month, day).AddDays(-2);
            ObservedHoliday obsdate = new ObservedHoliday(holiday);
            if (startDate == endDate && startDate.Day == observed.Day)
            {
                obsdate.DateObserved = observed;
                observedHolidays.Add(obsdate);
            }
            else if (startDate != endDate && observed >= startDate)
            {
                obsdate.DateObserved = observed;
                observedHolidays.Add(obsdate);
            }
        }
解决方案

For Chinese New Year, I think this would work:

ChineseLunisolarCalendar chinese   = new ChineseLunisolarCalendar();
GregorianCalendar        gregorian = new GregorianCalendar();

DateTime utcNow = DateTime.UtcNow;

// Get Chinese New Year of current UTC date/time
DateTime chineseNewYear = chinese.ToDateTime( utcNow.Year, 1, 1, 0, 0, 0, 0 );

// Convert back to Gregorian (you could just query properties of `chineseNewYear` directly, but I prefer to use `GregorianCalendar` for consistency:

Int32 year  = gregorian.GetYear( chineseNewYear );
Int32 month = gregorian.GetMonth( chineseNewYear );
Int32 day   = gregorian.GetDayOfMonth( chineseNewYear );

这篇关于算法找到中国春节的公历日期一定阳历一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:41