mysql 计算农历_公历转换农历算法

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceAlgorithmPractice

{public partial class公历转农历 : Form

{public公历转农历()

{

InitializeComponent();

}private void 公历转农历_Load(objectsender, 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 voidBindYear()

{

List arrYear = new List();for (int i = 0; i <= 2101 - 1901; i++)

{

arrYear.Insert(i,1901 +i);

}this.cbYear.DataSource =arrYear;

}//月份随年份而变

private void cbYear_SelectedIndexChanged(objectsender, 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(objectsender, 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);

}

}///

///公历转为农历的函数///

/// 公历日期

/// 农历的日期

public static stringSolarToChineseLunisolarDate(DateTime solarDateTime)

{//微软的ChineseLunisolarCalendar 方法支持时间范围是[1901-2-19,2101-1-28]

System.Globalization.ChineseLunisolarCalendar cal = newSystem.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(objectsender, 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);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值