c#中國農曆時間類

來源:互聯網
上載者:User

原來還準備自己寫演算法,並研究農曆規則。發現那太難和麻煩了,光是農曆的推算那就我等專門研究曆法的人一下搞懂的。後來發現。NET類庫也提供一些基礎的農曆類System.Globalization.ChineseLunisolarCalendar。我改裝了一下如DateTime時間形式。代碼如下。實現了 西曆農曆轉換的功能。但是只能算到1900~2100年之間的。基本夠日常使用了。原始碼如下。
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace System
    ...{
    /**//// <summary>
    /// 中國常用農曆日期時間類
    /// </summary>
    class ChinaDateTime
    ...{
        private int year, month, dayOfMonth;
        private bool isLeap;
        public DateTime time;

        /**//// <summary>
        /// 擷取當前日期的農曆年
        /// </summary>
        public int Year
        ...{
            get ...{ return year; }
        }

        /**//// <summary>
        /// 擷取當前日期的農曆月份
        /// </summary>
        public int Month
        ...{
            get ...{ return month; }
        }

        /**//// <summary>
        /// 擷取當前日期的農曆月中天數
        /// </summary>
        public int DayOfMonth
        ...{
            get ...{ return dayOfMonth; }
        }

        /**//// <summary>
        /// 擷取當前日期是否為閏月中的日期
        /// </summary>
        public bool IsLeap
        ...{
            get ...{ return isLeap; }
        }

        System.Globalization.ChineseLunisolarCalendar cc;
        /**//// <summary>
        /// 返回指定西曆日期的陰曆時間
        /// </summary>
        /// <param name="time"></param>
        public ChinaDateTime(DateTime time)
        ...{
            cc = new System.Globalization.ChineseLunisolarCalendar();
             if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime)
                throw new Exception("參數日期時間不在支援的範圍內,支援範圍:" + cc.MinSupportedDateTime.ToShortDateString()+"到"+cc.MaxSupportedDateTime.ToShortDateString());
            year = cc.GetYear(time);
            month = cc.GetMonth(time);
            dayOfMonth = cc.GetDayOfMonth(time);
            isLeap = cc.IsLeapMonth(year, month);
            if (isLeap) month -= 1;
            this.time = time;

        }

        /**//// <summary>
        /// 返回當前日前的農曆日期。
        /// </summary>
        public static ChinaDateTime Now
        ...{
            get ...{ return new ChinaDateTime(DateTime.Now); }
        }

        /**//// <summary>
        /// 返回指定農曆年,月,日,是否為閏月的農曆日期時間
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <param name="DayOfMonth"></param>
        /// <param name="IsLeap"></param>
        public ChinaDateTime(int Year, int Month, int DayOfMonth, bool IsLeap)
        ...{
            if (Year >= cc.MaxSupportedDateTime.Year || Year <= cc.MinSupportedDateTime.Year)
                throw new Exception("參數年份時間不在支援的範圍內,支援範圍:" + cc.MinSupportedDateTime.ToShortDateString() + "到" + cc.MaxSupportedDateTime.ToShortDateString());

            if (Month < 1 || Month > 12)
                throw new Exception("月份必須在1~12範圍");
            cc = new System.Globalization.ChineseLunisolarCalendar();
            if(cc.GetLeapMonth(Year)!=Month&&IsLeap)
                throw new Exception("指定的月份不是當年的閏月");
            if (cc.GetDaysInMonth(Year, IsLeap ? Month + 1 : Month) < DayOfMonth || DayOfMonth < 1)
                throw new Exception("指定的月中的天數不在當前月天數有效範圍");
            year = Year;
            month = Month;
            dayOfMonth = DayOfMonth;
            isLeap = IsLeap;
            time = DateTime.Now;
        }

        /**//// <summary>
        /// 擷取當前農曆日期的西曆時間
        /// </summary>
        public DateTime ToDateTime()
        ...{
            return cc.ToDateTime(year, isLeap ? month + 1 : month, dayOfMonth, time.Hour, time.Minute, time.Second, time.Millisecond);
        }

        /**//// <summary>
        /// 擷取指定農曆時間對應的西曆時間
        /// </summary>
        /// <param name="CnTime"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(ChinaDateTime CnTime)
        ...{
            return CnTime.ToDateTime();
        }

        /**//// <summary>
        /// 擷取指定西曆時間轉換為農曆時間
        /// </summary>
        /// <param name="Time"></param>
        /// <returns></returns>
        public static ChinaDateTime ToChinaDateTime(DateTime Time)
        ...{
            return new ChinaDateTime(Time);
        }

        /**//// <summary>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.