博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
公历转换农历算法
阅读量:6186 次
发布时间:2019-06-21

本文共 6570 字,大约阅读时间需要 21 分钟。

周末试着做了一个公历转农历的例子,执行结果如下:

代码如下:

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
arrYear = new List
(); 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); } } ///
/// 公历转为农历的函数 /// ///
公历日期 ///
农历的日期
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); } } }}

 

 

转载于:https://www.cnblogs.com/yinluhui0229/archive/2012/08/03/2622074.html

你可能感兴趣的文章
未来几年,BCH超越BTC的路径是什么?
查看>>
import和require的区别
查看>>
一个离开学校三年java架构师
查看>>
页面优化小总结 (图片类型)
查看>>
mysql中sum()与if()联合使用
查看>>
vue-resource安装与应用
查看>>
React编程规范
查看>>
面试题系列:你的系统如何支撑高并发?
查看>>
什么是代理模式?
查看>>
iOS KVC与KVO
查看>>
秋招总结:一篇文章搞定秋招学习规划
查看>>
antd Form组件方法getFieldsValue获取自定义组件的值
查看>>
python爬虫系列(3.2-lxml库的使用)
查看>>
我们统计了比特币的319次死亡详情,并预测了下一次
查看>>
SEO提高网站排名快速见效的方法
查看>>
(十五) 构建springmvc+mybatis+dubbo分布式平台-window安装dubbo管控台
查看>>
Mvp官方示例
查看>>
密码学基础(三)密码分析
查看>>
我的友情链接
查看>>
软件发布实践
查看>>