日期:2014-05-16 浏览次数:20949 次
WindowsPhone 中 自定义 类,根据公历 获取农历日期数据
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChineseCalendar { /// <summary> /// 中国农历 /// </summary> public static class ChineseDate { #region 日历数据 /// <summary> /// int[] CalendarData /// </summary> static int[] CalendarData = new int[] { 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95 }; /// <summary> /// int[] madd /// </summary> static int[] madd = new int[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; static String numString = "一二三四五六七八九十"; static String monString = "正二三四五六七八九十冬腊"; static String[] run = new String[] { "闰正", "闰二", "闰三", "闰四", "闰五", "闰六", "闰七", "闰八", "闰九", "闰十", "闰冬", "闰腊" }; static String cDateString = String.Empty; static int cYear, cMonth, cDay, cHour; #endregion #region 计算方法 /// <summary> /// int GetBit /// </summary> /// <param name="m"></param> /// <param name="n"></param> /// <returns></returns> private static int GetBit(int m, int n) { return (m >> n) & 1; } /// <summary> /// void e2c /// </summary> /// <param name="dt">公历时间</param> private static void e2c(DateTime dt) { bool isEnd = false; int total, m, n, k, tmp = dt.Year; if (tmp < 1900) tmp += 1900; total = (tmp - 2001) * 365 + (int)Math.Floor((Double)(tmp - 2001) / 4) + madd[dt.Month - 1] + dt.Day - 23; if (dt.Year % 4 == 0 && dt.Month > 2) total++; for (m = 0; ; m++) { k = (CalendarData[m] < 0xfff) ? 11 : 12; for (n = k; n >= 0; n--) { if (total <= 29 + GetBit(CalendarData[m], n)) { isEnd = true; break; } total = total - 29 - GetBit(CalendarData[m], n); } if (isEnd) break; } cYear = 2001 + m; cMonth = k - n + 1; cDay = total; if (k == 12) { if (cMonth == (int)Math.Floor((double)(CalendarData[m] / 0x10000) + 1)) cMonth = 1 - cMonth; if (cMonth > (int)Math.Floor((double)(CalendarData[m] / 0x10000) + 1)) cMonth--; } cHour = (int)Math.Floor((double)((dt.Hour + 3) / 2)); } /// <summary> /// void GetcDateString() /// </summary> private static void GetcDateString() { String tmp = String.Empty; if (cMonth < 1) { tmp += "闰"; tmp += monString[-cMonth - 1]; } else tmp += monString[cMonth - 1] + "月"; if (cDay <= 10) tmp += "初"; else if (cDay < 20) tmp += "十"; else if (cDay == 20) tmp += &