123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Text;
- namespace YiSha.Util
- {
- public class TKLunarCalendarHelper
- {
- //C# 获取农历日期
- ///<summary>
- /// 实例化一个 ChineseLunisolarCalendar
- ///</summary>
- private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
- ///<summary>
- /// 十天干
- ///</summary>
- private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
- ///<summary>
- /// 十二地支
- ///</summary>
- private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
- ///<summary>
- /// 十二生肖
- ///</summary>
- private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
- ///<summary>
- /// 返回农历天干地支年
- ///</summary>
- ///<param name="year">农历年</param>
- ///<return s></return s>
- public static string GetLunisolarYear(int year)
- {
- if (year > 3)
- {
- int tgIndex = (year - 4) % 10;
- int dzIndex = (year - 4) % 12;
- return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
- }
- throw new ArgumentOutOfRangeException("无效的年份!");
- }
- ///<summary>
- /// 农历月
- ///</summary>
- ///<return s></return s>
- private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
- ///<summary>
- /// 农历日
- ///</summary>
- private static string[] days1 = { "初", "十", "廿", "三" };
- ///<summary>
- /// 农历日
- ///</summary>
- private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
- private static int[] daysInt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- ///<summary>
- /// 返回农历月
- ///</summary>
- ///<param name="month">月份</param>
- ///<return s></return s>
- public static string GetLunisolarMonth(int month)
- {
- if (month < 13 && month > 0)
- {
- return months[month - 1];
- }
- throw new ArgumentOutOfRangeException("无效的月份!");
- }
- ///<summary>
- /// 返回农历日
- ///</summary>
- ///<param name="day">天</param>
- ///<return s></return s>
- public static string GetLunisolarDay(int day)
- {
- if (day > 0 && day < 32)
- {
- if (day != 20 && day != 30)
- {
- return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
- }
- else
- {
- return string.Concat(days[(day - 1) / 10], days1[1]);
- }
- }
- throw new ArgumentOutOfRangeException("无效的日!");
- }
- ///<summary>
- /// 根据公历获取农历日期
- ///</summary>
- ///<param name="datetime">公历日期</param>
- ///<return s></return s>
- public static string GetChineseDateTime(DateTime datetime)
- {
- int year = ChineseCalendar.GetYear(datetime);
- int month = ChineseCalendar.GetMonth(datetime);
- int day = ChineseCalendar.GetDayOfMonth(datetime);
- //获取闰月, 0 则表示没有闰月
- int leapMonth = ChineseCalendar.GetLeapMonth(year);
- bool isleap = false;
- if (leapMonth > 0)
- {
- if (leapMonth == month)
- {
- //闰月
- isleap = true;
- month--;
- }
- else if (month > leapMonth)
- {
- month--;
- }
- }
- return string.Concat(isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
- }
- public static bool IsHaveNewYear(DateTime datetime)
- {
- int year = ChineseCalendar.GetYear(datetime);
- int month = ChineseCalendar.GetMonth(datetime);
- int day = ChineseCalendar.GetDayOfMonth(datetime);
- //获取闰月, 0 则表示没有闰月
- int leapMonth = ChineseCalendar.GetLeapMonth(year);
- bool isleap = false;
- if (leapMonth > 0)
- {
- if (leapMonth == month)
- {
- //闰月
- isleap = true;
- month--;
- }
- else if (month > leapMonth)
- {
- month--;
- }
- }
- if (GetLunisolarMonth(month) != "正")
- {
- return true;
- }
- else
- {
- if (new List<string> { "初一", "初二", "初三", "初四", "初五", "初六", "初七" }.Contains(GetLunisolarDay(day)))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- /// <summary>
- /// 获取除夕日期
- /// </summary>
- /// <param name="year"></param>
- /// <returns></returns>
- public static DateTime GetNewYear(int year)
- {
- DateTime result = new DateTime();
- try
- {
- if (year < 1900 || year > 2100)
- {
- Exception e = new Exception("参数年份时间不在支持的范围内,支持范围1900~2100年");
- throw e;
- }
- //摘要:在1月21日-2月20日的31天中,历史上都有做过春节的经历,其中1月21日最少为1次,1月23日、2月6日、2月10日、2月17日最多为7次。
- DateTime t1 = new DateTime(year, 1, 21);
- DateTime t2 = new DateTime(year + 1, 2, 20);
- result = t1;
- ChineseLunisolarCalendar cc = new ChineseLunisolarCalendar();
- int t = t1.CompareTo(t2);
- while (t1.CompareTo(t2) != 0)
- {
- if (cc.GetMonth(t1) == 1 && cc.GetDayOfMonth(t1) == 1)
- {
- result = t1;
- }
- t1 = t1.AddDays(1);
- }
- return result;
- }
- catch (Exception err)
- {
- }
- return result;
- }
- }
- }
|