熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c#中國農歷時間類

2013-11-13 09:49:24  來源: .NET編程 

  原來還准備自己寫算法並研究農歷規則發現那太難和麻煩了光是農歷的推算那就我等專門研究歷法的人一下搞懂的後來發現NET類庫也提供一些基礎的農歷類SystemGlobalizationChineseLunisolarCalendar我改裝了一下如DateTime時間形式代碼如下實現了 公歷農歷轉換的功能但是只能算到~年之間的基本夠日常使用了源代碼如下
    using System;
    using SystemCollectionsGeneric;
    using SystemText;

  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; }
        }

  SystemGlobalizationChineseLunisolarCalendar cc;
        /**//// <summary>
        /// 返回指定公歷日期的陰歷時間
        /// </summary>
        /// <param name=time></param>
        public ChinaDateTime(DateTime time)
        {
            cc = new SystemGlobalizationChineseLunisolarCalendar();
           
             if (time > ccMaxSupportedDateTime || time < ccMinSupportedDateTime)
                throw new Exception(參數日期時間不在支持的范圍內支持范圍 + ccMinSupportedDateTimeToShortDateString()++ccMaxSupportedDateTimeToShortDateString());
            year = ccGetYear(time);
            month = ccGetMonth(time);
            dayOfMonth = ccGetDayOfMonth(time);
            isLeap = ccIsLeapMonth(year month);
            if (isLeap) month = ;
            thistime = time;

  }


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

  /**//// <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 >= ccMaxSupportedDateTimeYear || Year <= ccMinSupportedDateTimeYear)
                throw new Exception(參數年份時間不在支持的范圍內支持范圍 + ccMinSupportedDateTimeToShortDateString() + + ccMaxSupportedDateTimeToShortDateString());

  if (Month < || Month > )
                throw new Exception(月份必須在~范圍);
            cc = new SystemGlobalizationChineseLunisolarCalendar();
           
            if(ccGetLeapMonth(Year)!=Month&&IsLeap)
                throw new Exception(指定的月份不是當年的閏月);
            if (ccGetDaysInMonth(Year IsLeap ? Month + : Month) < DayOfMonth || DayOfMonth < )
                throw new Exception(指定的月中的天數不在當前月天數有效范圍);
            year = Year;
            month = Month;
            dayOfMonth = DayOfMonth;
            isLeap = IsLeap;
            time = DateTimeNow;
        }

  /**//// <summary>
        /// 獲取當前農歷日期的公歷時間
        /// </summary>
        public DateTime ToDateTime()
        {
            return ccToDateTime(year isLeap ? month + : month dayOfMonth timeHour timeMinute timeSecond timeMillisecond);
        }

  /**//// <summary>
        /// 獲取指定農歷時間對應的公歷時間
        /// </summary>
        /// <param name=CnTime></param>
        /// <returns></returns>
        public static DateTime ToDateTime(ChinaDateTime CnTime)
        {
            return CnTimeToDateTime();
        }

  /**//// <summary>
        /// 獲取指定公歷時間轉換為農歷時間
        /// </summary>
        /// <param name=Time></param>
        /// <returns></returns>
        public static ChinaDateTime ToChinaDateTime(DateTime Time)
        {
            return new ChinaDateTime(Time);
        }

  /**//// <summary>


From:http://tw.wingwit.com/Article/program/net/201311/11671.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.