c#输出农历转化及中文星期转化,string类型转化时间类型格式
来源:互联网 发布:嵌入式软件出口退税 编辑:程序博客网 时间:2024/05/21 15:39
///<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 > 3)
{
int tgIndex = (year - 4) % 10;
int dzIndex = (year - 4) % 12;
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 < 13 && month > 0)
{
return months[month - 1];
}
throw new ArgumentOutOfRangeException("无效的月份!");
}
///<summary>
/// 返回农历日
///</summary>
///<param name="day">天</param>
///<return s></return s>
public static string GetLunisolarDay(int day)
{
if (day > 0 && day < 32)
{
if (day != 20 && day != 30)
{
return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
}
else
{
return string.Concat(days[(day - 1) / 10], days1[1]);
}
}
throw new ArgumentOutOfRangeException("无效的日!");
}
public static string GetWeek()
{
string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
return week;
}
///<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 > 0)
{
if (leapMonth == month)
{
//闰月
isleap = true;
month--;
}
else if (month > leapMonth)
{
month--;
}
}
// return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day),GetWeek());获取农历日期和星期几:丙申[猴]年十一月三十星期三
return string.Concat( isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
/// 实例化一个 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 > 3)
{
int tgIndex = (year - 4) % 10;
int dzIndex = (year - 4) % 12;
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 < 13 && month > 0)
{
return months[month - 1];
}
throw new ArgumentOutOfRangeException("无效的月份!");
}
///<summary>
/// 返回农历日
///</summary>
///<param name="day">天</param>
///<return s></return s>
public static string GetLunisolarDay(int day)
{
if (day > 0 && day < 32)
{
if (day != 20 && day != 30)
{
return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
}
else
{
return string.Concat(days[(day - 1) / 10], days1[1]);
}
}
throw new ArgumentOutOfRangeException("无效的日!");
}
public static string GetWeek()
{
string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
return week;
}
///<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 > 0)
{
if (leapMonth == month)
{
//闰月
isleap = true;
month--;
}
else if (month > leapMonth)
{
month--;
}
}
// return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day),GetWeek());获取农历日期和星期几:丙申[猴]年十一月三十星期三
return string.Concat( isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
}
日期string类型转化
//DateTime dt = Convert.ToDateTime("2006050100");
DateTime ds = DateTime.ParseExact(tt, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
_sysj.SobservTimes = ds.ToString("yyyy年MM月dd日 HH时mm分");
0 0
- c#输出农历转化及中文星期转化,string类型转化时间类型格式
- String类型的转化为时间类型
- clob转化String类型
- String[] Object类型转化
- 字符串date类型格式时间串转化
- C# string类型和Byte[]相互转化
- C#类型转化问题
- C#类型转化
- <java>获取时间及中文格式转化
- Java时间类型和String类型间各种格式的转化
- 类型转化
- 类型转化
- 类型转化
- 类型转化
- 类型转化
- C# 将String类型转化成任意类型
- string 转化为 char *类型
- CString类型转化为string
- 后台保活方案
- 描点与坐标
- Java常用知识点
- Ajax 后台正确执行 但在前台获取不到返回值 但打断点就可以
- OBJECT-C的编码风格跟语法格式看的好蛋疼
- c#输出农历转化及中文星期转化,string类型转化时间类型格式
- apache kafka配置中request.required.acks含义
- Android下拖动任意View代码(使用属性动画,眼前一亮)
- java通过jdbc连接impala和pom.xml以及增查操作
- ios 代码块 异步回调
- Linux的五个查找命令
- NSObject中的类方法copyWithZone问题
- VS2013类向导“异常来自 HRESULT:0x8CE0000B”错误解决方法
- Bean property 'dao' is not writable or has an invalid setter method. Does the parameter type of the