zoukankan      html  css  js  c++  java
  • C# 将系统时间转换成农历时间

    直接上代码:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Text;
      4 
      5 namespace RSMISClient
      6 {
      7     using System.Globalization;
      8     public static class ChineseDate
      9     {
     10         private static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();
     11         //cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28
     12         //cCalendar.MinSupportedDateTime  返回支持的最小日期,即1901-2-19
     13 
     14         #region 农历年
     15         /// <summary>
     16         /// 十天干
     17         /// </summary>
     18         private static string[] tiangan = { "", "", "", "", "", "", "", "", "", "" };
     19 
     20         /// <summary>
     21         /// 十二地支
     22         /// </summary>
     23         private static string[] dizhi = { "", "", "", "", "", "", "", "", "", "", "", "" };
     24 
     25         /// <summary>
     26         /// 十二生肖
     27         /// </summary>
     28         private static string[] shengxiao = { "", "", "", "", "", "", "", "", "", "", "", "" };
     29 
     30         /// <summary>
     31         /// 返回农历天干地支年 
     32         /// </summary>
     33         /// <param name="year">农历年</param>
     34         /// <returns></returns>
     35         public static string GetLunisolarYear(int year)
     36         {
     37             if (year > 3)
     38             {
     39                 int tgIndex = (year - 4) % 10;
     40                 int dzIndex = (year - 4) % 12;
     41 
     42                 return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "[", shengxiao[dzIndex], "]");
     43             }
     44 
     45             throw new ArgumentOutOfRangeException("无效的年份!");
     46         }
     47         #endregion
     48 
     49         #region 农历月
     50         /// <summary>
     51         /// 农历月
     52         /// </summary>
     53         private static string[] months = { "", "", "", "", "", "", "", "", "", "", "十一(冬)", "十二(腊)" };
     54 
     55         /// <summary>
     56         /// 返回农历月
     57         /// </summary>
     58         /// <param name="month">月份</param>
     59         /// <returns></returns>
     60         public static string GetLunisolarMonth(int month)
     61         {
     62             if (month < 13 && month > 0)
     63             {
     64                 return months[month - 1];
     65             }
     66 
     67             throw new ArgumentOutOfRangeException("无效的月份!");
     68         }
     69         #endregion
     70 
     71         #region 农历日
     72         /// <summary>
     73         /// 
     74         /// </summary>
     75         private static string[] days1 = { "", "", "廿", "" };
     76 
     77         /// <summary>
     78         /// 79         /// </summary>
     80         private static string[] days = { "", "", "", "", "", "", "", "", "", "" };
     81 
     82         /// <summary>
     83         /// 返回农历日
     84         /// </summary>
     85         /// <param name="day"></param>
     86         /// <returns></returns>
     87         public static string GetLunisolarDay(int day)
     88         {
     89             if (day > 0 && day < 32)
     90             {
     91                 if (day != 20 && day != 30)
     92                 {
     93                     return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
     94                 }
     95                 else
     96                 {
     97                     return string.Concat(days[(day - 1) / 10], days1[1]);
     98                 }
     99             }
    100 
    101             throw new ArgumentOutOfRangeException("无效的日!");
    102         }
    103         #endregion
    104 
    105         #region 方法
    106         public static string GetChineseDateTime(DateTime datetime)
    107         {
    108             int lyear = cCalendar.GetYear(datetime);
    109             int lmonth = cCalendar.GetMonth(datetime);
    110             int lday = cCalendar.GetDayOfMonth(datetime);
    111 
    112             //获取闰月, 0 则表示没有闰月
    113             int leapMonth = cCalendar.GetLeapMonth(lyear);
    114             bool isleap = false;
    115 
    116             if (leapMonth > 0)
    117             {
    118                 if (leapMonth == lmonth)
    119                 {
    120                     //闰月
    121                     isleap = true;
    122                     lmonth--;
    123                 }
    124                 else if (lmonth > leapMonth)
    125                 {
    126                     lmonth--;
    127                 }
    128             }
    129 
    130             return string.Concat(GetLunisolarYear(lyear), "", isleap ? "" : string.Empty, GetLunisolarMonth(lmonth), "月·", GetLunisolarDay(lday));
    131         }
    132         #endregion
    133     }
    134 
    135 }
  • 相关阅读:
    【MVC 4】7.SportsSore:完成购物车
    【MVC 4】6.SportsSore:导航
    【MVC 4】5.SportsSore —— 一个真实的应用程序
    【网络文摘】面试感悟:3年工作经验程序员应有的技能
    【网络文摘】一个大神程序员的使命感究竟应该是什么
    join的简单总结
    模块化(1):基本思路
    Android 9.0新特性
    DataBinding初认识
    Android 7.0 新特性
  • 原文地址:https://www.cnblogs.com/Jins/p/3122732.html
Copyright © 2011-2022 走看看