using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Text; /// <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;
#region 构造函数 public ChineseCalendarInfo()
: this(DateTime.Now.Date)
{ } /// <summary>
/// 从指定的阳历日期创建中国日历信息实体类
/// </summary>
/// <param name="date">指定的阳历日期</param>
public ChineseCalendarInfo(DateTime date)
{
m_SolarDate = date;
LoadFromSolarDate();
} private void LoadFromSolarDate()
{
m_IsLeapMonth = false;
m_LunarYearSexagenary = null;
m_LunarYearAnimal = null;
m_LunarYearText = null;
m_LunarMonthText = null;
m_LunarDayText = null;
m_SolarWeekText = null;
m_SolarConstellation = null;
m_SolarBirthStone = null; m_LunarYear = calendar.GetYear(m_SolarDate);
m_LunarMonth = calendar.GetMonth(m_SolarDate);
int leapMonth = calendar.GetLeapMonth(m_LunarYear); if (leapMonth == m_LunarMonth)
{
m_IsLeapMonth = true;
m_LunarMonth -= ;
}
else if (leapMonth > && leapMonth < m_LunarMonth)
{
m_LunarMonth -= ;
} m_LunarDay = calendar.GetDayOfMonth(m_SolarDate); CalcConstellation(m_SolarDate, out m_SolarConstellation, out m_SolarBirthStone);
} #endregion
#region 日历属性 /// <summary>
/// 阳历日期
/// </summary>
public DateTime SolarDate
{
get { return m_SolarDate; }
set
{
if (m_SolarDate.Equals(value))
return;
m_SolarDate = value;
LoadFromSolarDate();
}
}
/// <summary>
/// 星期几
/// </summary>
public string SolarWeekText
{
get
{
if (string.IsNullOrEmpty(m_SolarWeekText))
{
int i = (int)m_SolarDate.DayOfWeek;
m_SolarWeekText = ChineseWeekName[i];
}
return m_SolarWeekText;
}
}
/// <summary>
/// 阳历星座
/// </summary>
public string SolarConstellation
{
get { return m_SolarConstellation; }
}
/// <summary>
/// 阳历诞生石
/// </summary>
public string SolarBirthStone
{
get { return m_SolarBirthStone; }
} /// <summary>
/// 阴历年份
/// </summary>
public int LunarYear
{
get { return m_LunarYear; }
}
/// <summary>
/// 阴历月份
/// </summary>
public int LunarMonth
{
get { return m_LunarMonth; }
}
/// <summary>
/// 是否阴历闰月
/// </summary>
public bool IsLeapMonth
{
get { return m_IsLeapMonth; }
}
/// <summary>
/// 阴历月中日期
/// </summary>
public int LunarDay
{
get { return m_LunarDay; }
} /// <summary>
/// 阴历年干支
/// </summary>
public string LunarYearSexagenary
{
get
{
if (string.IsNullOrEmpty(m_LunarYearSexagenary))
{
int y = calendar.GetSexagenaryYear(this.SolarDate);
m_LunarYearSexagenary = CelestialStem.Substring((y - ) % , ) + TerrestrialBranch.Substring((y - ) % , );
}
return m_LunarYearSexagenary;
}
}
/// <summary>
/// 阴历年生肖
/// </summary>
public string LunarYearAnimal
{
get
{
if (string.IsNullOrEmpty(m_LunarYearAnimal))
{
int y = calendar.GetSexagenaryYear(this.SolarDate);
m_LunarYearAnimal = Animals.Substring((y - ) % , );
}
return m_LunarYearAnimal;
}
} /// <summary>
/// 阴历年文本
/// </summary>
public string LunarYearText
{
get
{
if (string.IsNullOrEmpty(m_LunarYearText))
{
m_LunarYearText = Animals.Substring(calendar.GetSexagenaryYear(new DateTime(m_LunarYear, , )) % - , );
StringBuilder sb = new StringBuilder();
int year = this.LunarYear;
int d;
do
{
d = year % ;
sb.Insert(, ChineseNumber[d]);
year = year / ;
} while (year > );
m_LunarYearText = sb.ToString();
}
return m_LunarYearText;
}
}
/// <summary>
/// 阴历月文本
/// </summary>
public string LunarMonthText
{
get
{
if (string.IsNullOrEmpty(m_LunarMonthText))
{
m_LunarMonthText = (this.IsLeapMonth ? "闰" : "") + ChineseMonthName[this.LunarMonth - ];
}
return m_LunarMonthText;
}
} /// <summary>
/// 阴历月中日期文本
/// </summary>
public string LunarDayText
{
get
{
if (string.IsNullOrEmpty(m_LunarDayText))
m_LunarDayText = ChineseDayName[this.LunarDay - ];
return m_LunarDayText;
}
} #endregion /// <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 >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= || i <= )
j = ;
else if (i >= && i <= )
j = ;
else if (i >= && i <= )
j = ;
else
{
constellation = "未知星座";
birthstone = "未知诞生石";
return;
}
constellation = Constellations[j];
birthstone = BirthStones[j];
#region 星座划分
//白羊座: 3月21日------4月19日 诞生石: 钻石
//金牛座: 4月20日------5月20日 诞生石: 蓝宝石
//双子座: 5月21日------6月21日 诞生石: 玛瑙
//巨蟹座: 6月22日------7月22日 诞生石: 珍珠
//狮子座: 7月23日------8月22日 诞生石: 红宝石
//处女座: 8月23日------9月22日 诞生石: 红条纹玛瑙
//天秤座: 9月23日------10月23日 诞生石: 蓝宝石
//天蝎座: 10月24日-----11月21日 诞生石: 猫眼石
//射手座: 11月22日-----12月21日 诞生石: 黄宝石
//摩羯座: 12月22日-----1月19日 诞生石: 土耳其玉
//水瓶座: 1月20日-----2月18日 诞生石: 紫水晶
//双鱼座: 2月19日------3月20日 诞生石: 月长石,血石
#endregion
} #region 阴历转阳历 /// <summary>
/// 获取指定年份春节当日(正月初一)的阳历日期
/// </summary>
/// <param name="year">指定的年份</param>
private static DateTime GetLunarNewYearDate(int year)
{
DateTime dt = new DateTime(year, , );
int cnYear = calendar.GetYear(dt);
int cnMonth = calendar.GetMonth(dt); int num1 = ;
int num2 = calendar.IsLeapYear(cnYear) ? : ; while (num2 >= cnMonth)
{
num1 += calendar.GetDaysInMonth(cnYear, num2--);
} num1 = num1 - calendar.GetDayOfMonth(dt) + ;
return dt.AddDays(num1);
} /// <summary>
/// 阴历转阳历
/// </summary>
/// <param name="year">阴历年</param>
/// <param name="month">阴历月</param>
/// <param name="day">阴历日</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static DateTime GetDateFromLunarDate(int year, int month, int day, bool IsLeapMonth)
{
if (year < || year > )
throw new Exception("只支持1902~2100期间的农历年");
if (month < || month > )
throw new Exception("表示月份的数字必须在1~12之间"); if (day < || day > calendar.GetDaysInMonth(year, month))
throw new Exception("农历日期输入有误"); int num1 = , num2 = ;
int leapMonth = calendar.GetLeapMonth(year); if (((leapMonth == month + ) && IsLeapMonth) || (leapMonth > && leapMonth <= month))
num2 = month;
else
num2 = month - ; while (num2 > )
{
num1 += calendar.GetDaysInMonth(year, num2--);
} DateTime dt = GetLunarNewYearDate(year);
return dt.AddDays(num1 + day - );
} /// <summary>
/// 阴历转阳历
/// </summary>
/// <param name="date">阴历日期</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static DateTime GetDateFromLunarDate(DateTime date, bool IsLeapMonth)
{
return GetDateFromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth);
} #endregion
#region 从阴历创建日历 /// <summary>
/// 从阴历创建日历实体
/// </summary>
/// <param name="year">阴历年</param>
/// <param name="month">阴历月</param>
/// <param name="day">阴历日</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static ChineseCalendarInfo FromLunarDate(int year, int month, int day, bool IsLeapMonth)
{
DateTime dt = GetDateFromLunarDate(year, month, day, IsLeapMonth);
return new ChineseCalendarInfo(dt);
}
/// <summary>
/// 从阴历创建日历实体
/// </summary>
/// <param name="date">阴历日期</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static ChineseCalendarInfo FromLunarDate(DateTime date, bool IsLeapMonth)
{
return FromLunarDate(date.Year, date.Month, date.Day, IsLeapMonth);
} /// <summary>
/// 从阴历创建日历实体
/// </summary>
/// <param name="date">表示阴历日期的8位数字,例如:20070209</param>
/// <param name="IsLeapMonth">是否闰月</param>
public static ChineseCalendarInfo FromLunarDate(string date, bool IsLeapMonth)
{
Regex rg = new System.Text.RegularExpressions.Regex(@"^/d{7}(/d)$");
Match mc = rg.Match(date);
if (!mc.Success)
{
throw new Exception("日期字符串输入有误!");
}
DateTime dt = DateTime.Parse(string.Format("{0}-{1}-{2}", date.Substring(, ), date.Substring(, ), date.Substring(, )));
return FromLunarDate(dt, IsLeapMonth);
} #endregion 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[] { "钻石", "蓝宝石", "玛瑙", "珍珠", "红宝石", "红条纹玛瑙", "蓝宝石", "猫眼石", "黄宝石", "土耳其玉", "紫水晶", "月长石,血石" };
}

C#日期转换类的更多相关文章

  1. 日期转换类 DateConverter.java

    package com.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.D ...

  2. C++实现日期转换类DateTime

    概述 工作中我们在网络传输时使用time_t来传输时间,在显示时使用字符串来显示,下面是一个日期转换类的实现,方便以后使用: // DateTime.hpp #ifndef _DATETIME_H # ...

  3. java日期转换

    在java开发过程中,时间的转换时必须掌握的=========下面把时间转换做个总结,有可能不是很全面 时间格式只有两种 yyyy-MM-DD yyyy/MM/DD 时间的类型:字符串类型.sql类型 ...

  4. 日期Data类,日历类Calendar

    用于得到当前时间,和设置日期类数据 public void testDate() { // 创建一个日期对象 Date date = new Date(); /** * 从1900年1月1日 00:0 ...

  5. SpringMVC配置全局日期转换器,处理日期转换异常

    Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常链接地址: https://www.2cto.com/kf/201308/236837.html spring3.0配置日期转换可 ...

  6. 日期转换工具类 CommUtil.java

    package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...

  7. 时间日期转换工具类,获取当前时间YYYYMMDD24HHMISS、YYYYMMDDHHMISS

    YYYYMMDD24HHMISS:24小时制时间(显示上只是比YYYYMMDDHHMISS中间多了一个24),例:2018102224112440 YYYYMMDDHHMISS:12小时制时间,例20 ...

  8. coding++:java-Date日期转换工具类

    Code: package com.tree.ztree_demo.utils; import org.springframework.util.ObjectUtils; import java.te ...

  9. springMvc的日期转换之二

    方式一:使用@InitBinder注解实现日期转换 前台页面: 后台打印: 方式二:处理多种日期格式类型之间的转换 采用方式:由于binder.registerCustomEditor(Date.cl ...

随机推荐

  1. MTTR是什么?或者说为什么别给婴儿喝白兰地

    在团队纷纷谈起工作效率的时候,对运维工作者,他们通常喜欢用「故障的平均解决时间」来衡量团队的工作效率.然而这往往是不正确的.一个迅速解决大量突发事故的团队十分高效,而实际上这更有可能意味着该团队的基础 ...

  2. php使用domdocument读取xml文件

    使用domdocument读取xml文件需要用到以下几个方法和属性: 方法: 1:读取xml文件:load() 2:获取标签的对象数组:getElementByTagName() 3:对象数组的索引: ...

  3. Scarborough Fair 绝美天籁

    很少见的男声唱法,而且还古色古香: https://www.youtube.com/watch?v=sgbo2QWLBzI https://www.youtube.com/watch?v=-BakWV ...

  4. Java实现字符串反转

    替换原则:index k 的值和 n-k 的值进行交换.(始终记住程序员的n.k都是字符串的实际位置.) 乘除的最基本实现还是来源于移位操作. public String reverse(String ...

  5. poj3233

    这道题其实算是把快速幂的思想用在多项式之中 A+A^2+A^3+…+A^n=(A+A^1…+A^[n/2])+A^[n/2](A+A+A^1…+A^[n/2])+n mod 2*A^n 然后就是打码的 ...

  6. 【VirtualDOM】

    前沿技术解密——VirtualDOM miniflycn/qvd Matt-Esch/virtual-dom Facebook React 和 Web Components(Polymer)对比优势和 ...

  7. nodejs之日志管理

    开发一个项目时,可以通过控制台输出或者debug来获取到项目的运行信息.当项目上线时,我们就需要通过日志来分析.如同Java的log4j,nodejs中也有相关的log4js.使用过log4j的同学应 ...

  8. C语言基础课程 第四课 它山之石可以攻玉---C语言数据类型和表达式

     1         C语言中的数据类型 1.1      常量 常量就是在程序中不可变化的量 1.1.1         #define #define MAX 10 Define;//定义了一 ...

  9. 移动存储卡仍然用FAT32文件系统的真相

    微软在2001年就为自家的XP系统的本地磁盘默认使用了NTFS文件系统,但是12年之后,市面上的USB可移动设备和SD卡等外置存储器仍然在用着FAT32文件格式,这是什么理由让硬件厂商选择过时的文件系 ...

  10. ExecuteScalar的学习日志

    一:今天写关于调用sqlhelper类的时候出现了一个异常,我仔细观察没有错误啊,怎么回事:看图 二:出现错误时id的结果是0,也就是说ExcuteScalar的结果是null,明明数据库里有多行数据 ...