C#獲取到農曆相關(帶星座)(關於更多請參考我之前的一篇星座博客以及官網Api)

之前寫過一篇關於C#獲取到農曆相關的,今天又研究了一下,再寫一篇

 

代碼:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace LingbugUtils.Models
{
    public class ChineseCalendarModel
    {
        private string[] HeavenlyStemList;

        private string[] EarthlyBranchList;

        private string[] AnimalList;

        private string[] MonthList;

        private string[] DayFirstNameList;

        private string[] DayLastNameList;

        private ConstellationModel[] ConstellationList;

        public ChineseCalendarModel(DateTime date)
        {
            InitBaseData();
            Init(date);
        }

        private void InitBaseData()
        {
            char o = '、';
            //十天干
            string a = "甲、乙、丙、丁、戊、己、庚、辛、壬、癸";
            this.HeavenlyStemList = a.Split(o);
            //十二地支
            string b = "子、醜、寅、卯、辰、巳、午、未、申、酉、戌、亥";
            this.EarthlyBranchList = b.Split(o);
            //十二生肖
            string c = "鼠、牛、虎、兔、龍、蛇、馬、羊、猴、雞、狗、豬";
            this.AnimalList = c.Split(o);
            //月
            string d = "正、二、三、四、五、六、七、八、九、十、十一、臘";
            this.MonthList = d.Split(o);
            //日-開頭
            string e = "初、十、甘、三";
            this.DayFirstNameList = e.Split(o);
            //日-結尾
            string f = "一、二、三、四、五、六、七、八、九、十";
            this.DayLastNameList = f.Split(o);

            //星座
            var constellationList = new List<ConstellationModel>()
            {
                new ConstellationModel("白羊", "3-21", "4-19"),
                new ConstellationModel("金牛", "4-20", "5-20"),
                new ConstellationModel("雙子", "5-21", "6-21"),
                new ConstellationModel("巨蟹", "6-22", "7-22"),
                new ConstellationModel("獅子", "7-23", "8-22"),
                new ConstellationModel("處女", "8-23", "9-22"),
                new ConstellationModel("天秤", "9-23", "10-23"),
                new ConstellationModel("天蠍", "10-24", "11-22"),
                new ConstellationModel("射手", "11-23", "12-21"),
                new ConstellationModel("摩羯", "12-22", "1-19"),
                new ConstellationModel("水瓶", "1-20", "2-18"),
                new ConstellationModel("雙魚", "2-19", "3-20")
            };
            this.ConstellationList = constellationList.ToArray();
        }

        private void Init(DateTime date)
        {
            this.Argument = date;

            var calendarService = new ChineseLunisolarCalendar();

            //農曆年
            this.Year = calendarService.GetYear(this.Argument);
            //農曆月
            int month = calendarService.GetMonth(this.Argument);
            //今年是否有閏月
            if (calendarService.IsLeapYear(this.Year))
            {
                //有閏月

                //獲取到閏幾月(這裏-1是因爲,如果是7,則日曆會顯示閏6月,所以-1)
                int leapMonth = calendarService.GetLeapMonth(this.Year) - 1;
                //如果當前月比閏月大,都要-1(因爲多閏了一個月)
                this.Month = month > leapMonth ? month - 1 : month;
            }
            else
            {
                //沒有閏月,月份就是正確的
                this.Month = month;
            }
            //農曆日
            this.Day = calendarService.GetDayOfMonth(this.Argument);
            //農曆年索引(後續獲取天干地支生肖會使用到)
            int yearIndex = calendarService.GetSexagenaryYear(this.Argument);
            //天干
            this.HeavenlyStem = this.HeavenlyStemList[calendarService.GetCelestialStem(yearIndex) - 1];

            //地支和生肖索引一樣
            int animalIndex = calendarService.GetTerrestrialBranch(yearIndex) - 1;
            this.EarthlyBranch = this.EarthlyBranchList[animalIndex];
            this.Animal = this.AnimalList[animalIndex];

            //星座(因爲涉及到跨年,所以只要沒找到,那肯定是跨年的那個星座,就肯定是摩羯座)
            var constellationModel = this.ConstellationList.FirstOrDefault(r => this.Argument >= r.Begin && this.Argument <= r.End);
            this.ConstellationName = constellationModel == null ? "摩羯座" : constellationModel.Name;

            //月顯示
            string monthStr = this.MonthList[this.Month - 1];
            //如果是閏月,加上閏
            if (calendarService.IsLeapMonth(this.Year, month)) monthStr = "閏" + monthStr;

            //日顯示
            string dayStr = string.Empty;
            //整十的額外處理
            if (this.Day == 10)
            {
                dayStr = "初十";
            }
            else if (this.Day == 20)
            {
                dayStr = "二十";
            }
            else if (this.Day == 30)
            {
                dayStr = "三十";
            }
            else
            {
                //不是整十的
                int dayMod = this.Day % 10;//取餘,獲取到後面顯示
                int dayDev = this.Day / 10;//除法,獲取到前面顯示
                //拼接起來
                dayStr = this.DayFirstNameList[dayDev] + this.DayLastNameList[dayMod - 1];
            }

            //完整顯示
            this.Text = string.Format("{0}{1}{2}年{3}月{4}", this.HeavenlyStem, this.EarthlyBranch, this.Animal, monthStr, dayStr);
        }

        //參數日期
        public DateTime Argument { get; set; }

        //農曆年
        public int Year { get; set; }

        //農曆月
        public int Month { get; set; }

        //農曆日
        public int Day { get; set; }

        //天干
        public string HeavenlyStem { get; set; }

        //地支
        public string EarthlyBranch { get; set; }

        //生肖
        public string Animal { get; set; }

        //星座
        public string ConstellationName { get; set; }

        public string Text { get; set; }
    }
}

 

星座類:

using System;

namespace LingbugUtils.Models
{
    public class ConstellationModel
    {
        public ConstellationModel(string name, string beginStr, string endStr)
        {
            this.Name = name + "座";
            this.Begin = Convert.ToDateTime(Convert.ToDateTime(beginStr).ToString("yyyy-MM-dd 00:00:00.000"));
            this.End = Convert.ToDateTime(Convert.ToDateTime(endStr).ToString("yyyy-MM-dd 23:59:59.999"));
        }

        public string Name { get; set; }

        public DateTime Begin { get; set; }

        public DateTime End { get; set; }
    }
}

 

Ending~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章