C# 计算农历日期方法(2021版)
解决问题
旧版农历获取方法报错,会有
到 2021年 m数组越界了 if (LunarData[m] < 4095)
此方法可以解决
主体代码
public static class ChinaDate
{
private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar();
private static Hashtable gHoliday = new Hashtable();
private static Hashtable nHoliday = new Hashtable();
private static string[] JQ = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
private static int[] JQData = { 0, 21208, 43467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
static ChinaDate()
{
//公历节日
gHoliday.Add("0101", "元旦");
gHoliday.Add("0214", "情人节");
gHoliday.Add("0305", "雷锋日");
gHoliday.Add("0308", "妇女节");
gHoliday.Add("0312", "植树节");
gHoliday.Add("0315", "消费者权益日");
gHoliday.Add("0401", "愚人节");
gHoliday.Add("0501", "劳动节");
gHoliday.Add("0504", "青年节");
gHoliday.Add("0601", "儿童节");
gHoliday.Add("0701", "建党节");
gHoliday.Add("0801", "建军节");
gHoliday.Add("0910", "教师节");
gHoliday.Add("1001", "国庆节");
gHoliday.Add("1224", "平安夜");
gHoliday.Add("1225", "圣诞节");
//农历节日
nHoliday.Add("0101", "春节");
nHoliday.Add("0115", "元宵节");
nHoliday.Add("0505", "端午节");
nHoliday.Add("0815", "中秋节");
nHoliday.Add("0909", "重阳节");
nHoliday.Add("1208", "腊八节");
}
/// <summary>
/// 获取农历
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetChinaDate(DateTime dt)
{
if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime)
{
//日期范围:1901 年 2 月 19 日 - 2101 年 1 月 28 日
throw new Exception(string.Format("日期超出范围!必须在{0}到{1}之间!", china.MinSupportedDateTime.ToString("yyyy-MM-dd"), china.MaxSupportedDateTime.ToString("yyyy-MM-dd")));
}
string str = string.Format("{0} {1}{2}", GetYear(dt), GetMonth(dt), GetDay(dt));
string strJQ = GetSolarTerm(dt);
if (strJQ != "")
{
str += " (" + strJQ + ")";
}
string strHoliday = GetHoliday(dt);
if (strHoliday != "")
{
str += " " + strHoliday;
}
string strChinaHoliday = GetChinaHoliday(dt);
if (strChinaHoliday != "")
{
str += " " + strChinaHoliday;
}
return str;
}
/// <summary>
/// 获取农历年份
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetYear(DateTime dt)
{
int yearIndex = china.GetSexagenaryYear(dt);
string yearTG = " 甲乙丙丁戊己庚辛壬癸";
string yearDZ = " 子丑寅卯辰巳午未申酉戌亥";
string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪";
int year = china.GetYear(dt);
int yTG = china.GetCelestialStem(yearIndex);
int yDZ = china.GetTerrestrialBranch(yearIndex);
string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]);
return str;
}
/// <summary>
/// 获取农历月份
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetMonth(DateTime dt)
{
int year = china.GetYear(dt);
int iMonth = china.GetMonth(dt);
int leapMonth = china.GetLeapMonth(year);
bool isLeapMonth = iMonth == leapMonth;
if (leapMonth != 0 && iMonth >= leapMonth)
{
iMonth--;
}
string szText = "正二三四五六七八九十";
string strMonth = isLeapMonth ? "闰" : "";
if (iMonth <= 10)
{
strMonth += szText.Substring(iMonth - 1, 1);
}
else if (iMonth == 11)
{
strMonth += "十一";
}
else
{
strMonth += "腊";
}
return strMonth + "月";
}
/// <summary>
/// 获取农历日期
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetDay(DateTime dt)
{
int iDay = china.GetDayOfMonth(dt);
string szText1 = "初十廿三";
string szText2 = "一二三四五六七八九十";
string strDay;
if (iDay == 20)
{
strDay = "二十";
}
else if (iDay == 30)
{
strDay = "三十";
}
else
{
strDay = szText1.Substring((iDay - 1) / 10, 1);
strDay = strDay + szText2.Substring((iDay - 1) % 10, 1);
}
return strDay;
}
/// <summary>
/// 获取节气
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetSolarTerm(DateTime dt)
{
DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0);
DateTime dtNew;
double num;
int y;
string strReturn = "";
y = dt.Year;
for (int i = 1; i <= 24; i++)
{
num = 525948.76 * (y - 1900) + JQData[i - 1];
dtNew = dtBase.AddMinutes(num);
if (dtNew.DayOfYear == dt.DayOfYear)
{
strReturn = JQ[i - 1];
}
}
return strReturn;
}
/// <summary>
/// 获取公历节日
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetHoliday(DateTime dt)
{
string strReturn = "";
object g = gHoliday[dt.Month.ToString("00") + dt.Day.ToString("00")];
if (g != null)
{
strReturn = g.ToString();
}
return strReturn;
}
/// <summary>
/// 获取农历节日
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string GetChinaHoliday(DateTime dt)
{
string strReturn = "";
int year = china.GetYear(dt);
int iMonth = china.GetMonth(dt);
int leapMonth = china.GetLeapMonth(year);
int iDay = china.GetDayOfMonth(dt);
if (china.GetDayOfYear(dt) == china.GetDaysInYear(year))
{
strReturn = "除夕";
}
else if (leapMonth != iMonth)
{
if (leapMonth != 0 && iMonth >= leapMonth)
{
iMonth--;
}
object n = nHoliday[iMonth.ToString("00") + iDay.ToString("00")];
if (n != null)
{
if (strReturn == "")
{
strReturn = n.ToString();
}
else
{
strReturn += " " + n.ToString();
}
}
}
return strReturn;
}
}
调用方法:
ChinaDate.GetChinaDate(DateTime dt) //获取农历日期
ChinaDate.GetYear(DateTime dt) //获取农历年份(包含天干地支和生肖信息)
ChinaDate.GetMonth(DateTime dt) //获取农历月份
ChinaDate.GetDay(DateTime dt) //获取农历日期
ChinaDate.GetSolarTerm(DateTime dt) //获取节气
ChinaDate.GetHoliday(DateTime dt) //获取公历节日
C# 计算农历日期方法(2021版)的更多相关文章
- Anaconda 用于科学计算的 Python 发行版
用于科学计算的 Python 发行版: 1.Anaconda https://www.continuum.io/ 公司continuum. 有商业版本. Anaconda is the le ...
- 【Anaconda】:科学计算的Python发行版
[背景] Python易用,但包管理和Python不同版本的问题比较头疼,特别是当你使用Windows的时候.为了解决这些问题,有不少发行版的Python,比如WinPython.Anaconda等, ...
- java 日历计算农历和节假日的工具类
背景 业务需求需要后端提供这样的接口,网上找了很多java代码例子,虽然功能实现了 但是不完善,特别是节日那一块儿.然后百度发现有这样的插件,但是信息也是java后端提供的非js 然后在开源js插件找 ...
- IDEA 2021版新建Maven、TomCat工程
目录 2021版IDEA中Maven.TomCat配置 1.基于Webapp创建一个Maven项目 1.1 新建项目模板 1.2 指定名称 1.3 指定信息 1.4 指定Maven地址 1.5 构建成 ...
- SQL Server 中计算农历
1.建一表,放初始化资料 因为农历的日期,是由天文学家推算出来的,到现在只有到2049年的,以后的有了还可以加入! CREATE TABLE SolarData ( yearId int no ...
- 在 Qualys SSL Labs SSL 测试中获得 A+ 评级的秘技 2021 版
本系列文章将阐述主流应用交付控制器和主流 Web 服务器如何运行 HTTP/2 和 TLSv1.3 协议,以及如何在 SSL Test 中获得 A+ 评级. 请访问原文链接:https://sysin ...
- PAT 乙级 1063 计算谱半径(20) C++版
1063. 计算谱半径(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 在数学中,矩阵的“谱半径”是指其特 ...
- OpenACC 计算圆周率(简单版)
▶ 书上的计算圆周率的简单程序,主要是使用了自定义函数 #include <stdio.h> #include <stdlib.h> #include <math.h&g ...
- 复利计算C转java版
import java.util.Scanner; public class Compound_int { public static void main(String[] args) { tip() ...
随机推荐
- storm卡顿修改
最近的webstorm越来越卡了,有时候甚至会弹出 Out of memory的窗口,提示要设置 xmx的值, 8G内存跑你这小软件还会不够用???要内存?给你,看你还会不会卡成翔! 于是果断给x ...
- 题解 「BJOI2018 治疗之雨」
题目传送门 题目大意 有一个初始为 \(p\) 的数,每次操作分为以下两个: 有 \(\frac{1}{m+1}\) 的概率$+1,但是中途 \(p\) 的最大值只能为 \(n\)$ 有 \(k\) ...
- 洛谷4606 SDOI2018战略游戏(圆方树+虚树)
QWQ深受其害 当时在现场是真的绝望...... 现在再重新来看这个题 QWQ 根据题目所说,我们可以发现,对于每一个集合中的节点,我们实际上就是要求两两路径上的割点的数目 考虑到又是关于点双的题目, ...
- OutOfMemoryException异常解析
一.概述 在国庆休假快结束的最后一天晚上接到了部门老大的电话,某省的服务会出现崩溃问题.需要赶紧修复,没错这次的主角依旧是上次的"远古项目"没有办法同事都在休假没有人能帮忙开电脑远 ...
- 以太坊web3开发初步学习
以太坊web3开发初步学习 此文是对https://learnblockchain.cn/2018/04/15/web3-html/的学习再理解. 以太坊智能合约通过使用web3.js前端和智能合约交 ...
- python中常用的导包的方法和常用的库
python中常用的导包的方法 导入包和包名的方法:1.import package.module 2.from package.module import * 例一: ...
- MarkDown之Typora使用
Typora:所见即所得 常用快捷键 加粗:ctrl + B 标题:ctrl + 16,对于与16级标题 插入公式:ctrl + Shift + m 插入代码:ctrl + Shift + K 插入图 ...
- [对对子队]会议记录4.20(Scrum Meeting11)
今天已完成的工作 何瑞 工作内容:搭建第三关,添加了运行指令标识 相关issue:搭建关卡2.3 相关签入:4.20签入1 4.20签入2 吴昭邦 工作内容:搭建第三关 相关iss ...
- [对对子队]会议记录5.18(Scrum Meeting5)
今天已完成的工作 何瑞 工作内容:搭建第8关 相关issue:搭建关卡7.8.9 相关签入:feat:初步搭建了Lv8 吴昭邦 工作内容:搭建第8关 相关issue:搭建关卡7.8 ...
- Spring Authorization Server的使用
Spring Authorization Server的使用 一.背景 二.前置知识 三.需求 四.核心代码编写 1.引入授权服务器依赖 2.创建授权服务器用户 3.创建授权服务器和客户端 五.测试 ...