公历转换农历算法
周末试着做了一个公历转农历的例子,执行结果如下:
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AlgorithmPractice { public partial class 公历转农历 : Form { public 公历转农历() { InitializeComponent(); } private void 公历转农历_Load(object sender, EventArgs e) { BindYear(); this.cbYear.SelectedItem = DateTime.Now.Year; this.cbMonth.SelectedItem = DateTime.Now.Month; this.cbDay.SelectedItem = DateTime.Now.Day; btnChange_Click(sender,e); } //绑定年份 public void BindYear() { List<int> arrYear = new List<int>(); for (int i = 0; i <= 2101 - 1901; i++) { arrYear.Insert(i, 1901 + i); } this.cbYear.DataSource = arrYear; } //月份随年份而变 private void cbYear_SelectedIndexChanged(object sender, EventArgs e) { int[] year1901 = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; int[] year2101 = { 1 }; int[] yearOther = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; if (this.cbYear.Text == "1901") { this.cbMonth.DataSource = year1901; } else if (this.cbYear.Text == "2101") { this.cbMonth.DataSource = year2101; } else { this.cbMonth.DataSource = yearOther; } } //开始转换 private void btnChange_Click(object sender, EventArgs e) { try { string date = this.cbYear.Text + "-" + cbMonth.Text + "-" + cbDay.Text; DateTime dt = Convert.ToDateTime(date); this.lblNongLi.Text = SolarToChineseLunisolarDate(dt); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 公历转为农历的函数 /// </summary> /// <param name="solarDateTime">公历日期</param> /// <returns>农历的日期</returns> public static string SolarToChineseLunisolarDate(DateTime solarDateTime) { //微软的ChineseLunisolarCalendar 方法支持时间范围是[1901-2-19,2101-1-28] System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar(); int year = cal.GetYear(solarDateTime);//农历年份 int month = cal.GetMonth(solarDateTime);//有闰月时该值可能为13,即leapMonth <= month ? month - 1 : month表示实际农历月份 int day = cal.GetDayOfMonth(solarDateTime);//农历天数 int leapMonth = cal.GetLeapMonth(year);//此年份闰几月,闰n月则返回n+1,如闰4月返回值为5;没有闰月返回0 String impday = "";//一年中的农历节日 String leapMonthStr = leapMonth > 0 ? "(闰" + (leapMonth - 1).ToString() + "月)" : "";//闰月份 int monthtrue = leapMonth > 0 && leapMonth <= month ? month - 1 : month;//把闰月计算进去之后真正的农历月份 if (monthtrue == 1 && day == 1) { impday = "春节"; } else if (monthtrue == 5 && day == 5) { impday = "端午"; } else if (monthtrue == 8 && day == 15) { impday = "中秋"; } String month2 = String.Format("{0}{1}月", month == leapMonth ? "闰" : "" , "无正二三四五六七八九十冬腊"[monthtrue] ); String day2 = string.Format("{0}{1}" , "初十廿三"[day == 10 ? 0 : day / 10] , "十一二三四五六七八九"[day % 10] ); String date = year + "年" + month2 + day2; if (impday != "") { return date + leapMonthStr + " " + "祝您" + impday + "节快乐 ^-^"; } else return date + leapMonthStr; } //日子随月份改变 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.cbYear.Text.Trim())) { MessageBox.Show("年份不能为空!"); return; } try { int[] month31 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; int[] month30 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }; int[] month29 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 }; int[] month28 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 }; int[] month190102 = { 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 }; int[] month210101 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 }; String strmonth = this.cbMonth.Text.Trim(); String stryear = this.cbYear.Text.Trim(); if (stryear == "1901" && strmonth == "2") { this.cbDay.DataSource =month190102; } else if (stryear == "2101" && strmonth == "1") { this.cbDay.DataSource = month210101; } else { if (strmonth == "1" || strmonth == "3" || strmonth == "5" || strmonth == "7" || strmonth == "8" || strmonth == "10" || strmonth == "12") { this.cbDay.DataSource = month31; } else if (strmonth == "4" || strmonth == "9" || strmonth == "6" || strmonth == "11") { this.cbDay.DataSource = month30; } else if (strmonth == "2") { if (System.DateTime.IsLeapYear(Convert.ToInt32(this.cbYear.Text.Trim())) == true) { this.cbDay.DataSource = month29;//闰年 } else { this.cbDay.DataSource = month28; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
更多前端内容请访问个人博客:殷路辉的个人博客