//C# 获取农历日期

///<summary>
/// 实例化一个 ChineseLunisolarCalendar
///</summary>
private static ChineseLunisolarCalendar ChineseCalendar =new ChineseLunisolarCalendar();
 
///<summary>
/// 十天干
///</summary>
private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
 
///<summary>
/// 十二地支
///</summary>
private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
 
///<summary>
/// 十二生肖
///</summary>
private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
 
///<summary>
/// 返回农历天干地支年
///</summary>
///<param name="year">农历年</param>
///<return s></return s>
public static string GetLunisolarYear(int year)
{
if (year >)
{
int tgIndex = (year -) %;
int dzIndex = (year -) %;
 
return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
}
 
throw new ArgumentOutOfRangeException("无效的年份!");
}
 
///<summary>
/// 农历月
///</summary>
 
///<return s></return s>
private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
 
///<summary>
/// 农历日
///</summary>
private static string[] days1 = { "初", "十", "廿", "三" };
///<summary>
/// 农历日
///</summary>
private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
 
 
///<summary>
/// 返回农历月
///</summary>
///<param name="month">月份</param>
///<return s></return s>
public static string GetLunisolarMonth(int month)
{
if (month < && month >)
{
return months[month -];
}
 
throw new ArgumentOutOfRangeException("无效的月份!");
}
 
///<summary>
/// 返回农历日
///</summary>
///<param name="day">天</param>
///<return s></return s>
public static string GetLunisolarDay(int day)
{
if (day > && day <)
{
if (day != && day !=)
{
return string.Concat(days1[(day -) /], days[(day -) %]);
}
else
{
return string.Concat(days[(day -) /], days1[]);
  }
}
 
throw new ArgumentOutOfRangeException("无效的日!");
}
 
 
 
///<summary>
/// 根据公历获取农历日期
///</summary>
///<param name="datetime">公历日期</param>
///<return s></return s>
public static string GetChineseDateTime(DateTime datetime)
{
int year = ChineseCalendar.GetYear(datetime);
int month = ChineseCalendar.GetMonth(datetime);
int day = ChineseCalendar.GetDayOfMonth(datetime);
//获取闰月, 0 则表示没有闰月
int leapMonth = ChineseCalendar.GetLeapMonth(year);
 
bool isleap =false;
 
if (leapMonth >)
{
if (leapMonth == month)
{
//闰月
isleap =true;
month--;
}
else if (month > leapMonth)
{
  month--;
}
}
 
return string.Concat(GetLunisolarYear(year), "年", isleap ?"闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
}

最新文章

  1. 使用密码记录工具keepass来保存密码
  2. ASP.Net中的Web Resource
  3. Thinkphp去掉index.php
  4. c语言 指针与地址的区别
  5. 四、卫星定位《苹果iOS实例编程入门教程》
  6. 看StackOverflow如何用25台服务器撑起5.6亿的月PV(微软的架构)
  7. Python基础:数值(布尔型、整型、长整型、浮点型、复数)
  8. java中获取本地文件的编码
  9. cocos2dx中常见的类及类继承关系
  10. struts2面试题
  11. 用python写爬虫
  12. java全组合算法
  13. PAT (Advanced Level) 1081. Rational Sum (20)
  14. Mac OSX 搭建 Apache php mySql phpMyAdmin 开发环境
  15. 【一天一道LeetCode】#71. Simplify Path
  16. python--线程同步原语
  17. javascript之Map
  18. Iris分类以及数组reshape想到的
  19. Codeforces Round #541 (Div. 2)
  20. Linux各主要发行版的包管理命令对照

热门文章

  1. HA集群中namenode连接不上journalnode,导致namenode启动不了
  2. erlang节点局域网通信
  3. python学习笔记十七:base64及md5编码
  4. CSS系列(7)CSS类选择器Class详解
  5. android 摄像头相关使用记录
  6. 设计模式之模板方法模式 templateMethod
  7. 软工实践 - 第十五次作业 Alpha 冲刺 (6/10)
  8. android 继承ListView实现滑动删除功能.
  9. ftrace 简介
  10. java中从实体类中取值会忽略的的问题