C#实现的阴历阳历互相转化类实例
作者:songkexin 时间:2021-12-24 06:41:39
本文实例讲述了C#实现的阴历阳历互相转化类。分享给大家供大家参考,具体如下:
最近郁闷地发现网上现有的相当一部分万年历上干支纪年的算法都是错误的。因为干支纪年是针对阴历而言的,而生肖属相又跟地支对应,所以元旦和春节之间那段时间在干支纪年法中应该归上一年,以阳历2007年2月9日为例,当日的阴历日期是二〇〇六年十二月廿二日,是丙戌年,即狗年,但是浏览一下目前的万年历,相当一部分都显示成了丁亥年,猪年,比较郁闷~~
然后就写了一个阴历阳历互相转化的类。
相关代码如下:
/// <summary>
/// 中国日历信息实体类
/// </summary>
public sealed class ChineseCalendarInfo
{
private DateTime m_SolarDate;
private int m_LunarYear, m_LunarMonth, m_LunarDay;
private bool m_IsLeapMonth = false;
private string m_LunarYearSexagenary = null, m_LunarYearAnimal = null;
private string m_LunarYearText = null, m_LunarMonthText = null, m_LunarDayText = null;
private string m_SolarWeekText = null, m_SolarConstellation = null, m_SolarBirthStone = null;
日历属性
/// <summary>
/// 根据指定阳历日期计算星座&诞生石
/// </summary>
/// <param name="date">指定阳历日期</param>
/// <param name="constellation">星座</param>
/// <param name="birthstone">诞生石</param>
public static void CalcConstellation(DateTime date, out string constellation, out string birthstone)
{
int i = Convert.ToInt32(date.ToString("MMdd"));
int j;
if (i >= 321 && i <= 419)
j = 0;
else if (i >= 420 && i <= 520)
j = 1;
else if (i >= 521 && i <= 621)
j = 2;
else if (i >= 622 && i <= 722)
j = 3;
else if (i >= 723 && i <= 822)
j = 4;
else if (i >= 823 && i <= 922)
j = 5;
else if (i >= 923 && i <= 1023)
j = 6;
else if (i >= 1024 && i <= 1121)
j = 7;
else if (i >= 1122 && i <= 1221)
j = 8;
else if (i >= 1222 || i <= 119)
j = 9;
else if (i >= 120 && i <= 218)
j = 10;
else if (i >= 219 && i <= 320)
j = 11;
else
{
constellation = "未知星座";
birthstone = "未知诞生石";
return;
}
constellation = Constellations[j];
birthstone = BirthStones[j];
星座划分
}
阴历转阳历
从阴历创建日历
private static ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
public const string ChineseNumber = "〇一二三四五六七八九";
public const string CelestialStem = "甲乙丙丁戊己庚辛壬癸";
public const string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";
public const string Animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
public static readonly string[] ChineseWeekName = new string[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
public static readonly string[] ChineseDayName = new string[] {
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
public static readonly string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
public static readonly string[] Constellations = new string[] { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座" };
public static readonly string[] BirthStones = new string[] { "钻石", "蓝宝石", "玛瑙", "珍珠", "红宝石", "红条纹玛瑙", "蓝宝石", "猫眼石", "黄宝石", "土耳其玉", "紫水晶", "月长石,血石" };
}
附:完整实例代码点击此处本站下载。
PS:这里再为大家推荐几款日历相关在线工具供大家参考:
网页万年历日历:
http://tools.jb51.net/bianmin/webwannianli
在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli
在线万年历日历:
http://tools.jb51.net/bianmin/wannianli
在线万年历黄历flash版:
http://tools.jb51.net/bianmin/flashwnl
另外,本站历史上的今天也有相似的农历日期显示功能:
http://tools.jb51.net/bianmin/lishi
希望本文所述对大家C#程序设计有所帮助。
标签:C#,阴历,阳历
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现随机验证码图片生成
2022-12-08 02:27:00
![](https://img.aspxhome.com/file/2023/1/82301_0s.jpg)
Java编程实现判断网上邻居文件是否存在的方法
2022-12-27 09:49:46
浅析C# 函数的传值与传址
2023-11-22 04:46:57
![](https://img.aspxhome.com/file/2023/0/71730_0s.png)
IntelliJ IDEA 常用设置(配置)吐血整理(首次安装必需)
2021-06-24 15:23:49
![](https://img.aspxhome.com/file/2023/8/84278_0s.png)
一键设置java 环境变量 cmd下查看、修改(覆盖与添加)等说明
2023-03-29 07:09:25
![](https://img.aspxhome.com/file/2023/3/82033_0s.png)
详解Java中final的用法
2022-09-22 10:20:32
Spring Security 强制退出指定用户的方法
2022-10-04 18:13:04
maven手动上传jar包示例及图文步骤过程
2023-11-13 22:10:57
![](https://img.aspxhome.com/file/2023/1/59571_0s.png)
Java性能优化技巧汇总
2023-01-05 16:26:31
Vs2022环境下安装低版本.net framework的实现步骤
2023-07-04 02:58:12
![](https://img.aspxhome.com/file/2023/8/78988_0s.png)
Java Web实现简易图书管理系统
2023-12-17 21:48:34
![](https://img.aspxhome.com/file/2023/2/85432_0s.jpg)
javaweb登录验证码的实现方法
2021-09-12 05:00:08
![](https://img.aspxhome.com/file/2023/5/77985_0s.jpg)
Java中List与Map初始化的一些写法分享
2021-11-23 13:13:46
Java二维数组实现数字拼图效果
2021-11-21 20:39:17
Android 无障碍全局悬浮窗实现示例
2023-07-30 08:50:01
深入理解ThreadLocal工作原理及使用示例
2022-02-27 19:24:14
![](https://img.aspxhome.com/file/2023/6/62066_0s.png)
RocketMQ消息过滤与查询的实现
2023-06-26 10:04:25
![](https://img.aspxhome.com/file/2023/0/68700_0s.jpg)
详解springboot测试类注解
2023-06-04 21:04:37
![](https://img.aspxhome.com/file/2023/6/71246_0s.png)
android压力测试命令monkey详解
2023-06-17 00:36:29
MyBatis-Plus联表查询以及分页代码实例
2023-11-26 01:51:32
![](https://img.aspxhome.com/file/2023/8/60028_0s.png)