C#公历转农历

在.net2.0 的版本中的 System.Globalization.ChineseLunisolarCalendar 是针对中国的日历类,公历与中国传统农历纪年之间的相互转换,利用它可以计算天干地支等有关农历的信息。

使用

static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();
cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28
cCalendar.MinSupportedDateTime  返回支持的最小日期,即1901-2-19
 
下面我们来实现公历转农历。
/// <summary> /// 根据公历获取农历日期 /// </summary> /// <param name="datetime">公历日期</param> /// <returns></returns> public static string GetChineseDateTime(DateTime datetime)
{
    int lyear = cCalendar.GetYear(datetime);
    int lmonth = cCalendar.GetMonth(datetime);
    int lday = cCalendar.GetDayOfMonth(datetime);

    //获取闰月, 0 则表示没有闰月 int leapMonth = cCalendar.GetLeapMonth(lyear);

    bool isleap = false;

    if (leapMonth > 0)
    {
        if (leapMonth == lmonth)
        {
            //闰月 isleap = true;
            lmonth--;
        }
        else if (lmonth > leapMonth)
        {
            lmonth--;
        }
    }

    return string.Concat(GetLunisolarYear(lyear), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday));
}

测试的结果:

传入日期:2010-3-4

返回农历:庚寅[虎]年正月十九

可以满足简单的需求啦。

其他代码也附上:

#region 农历年

/// <summary> /// 十天干 /// </summary> private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

/// <summary> /// 十二地支 /// </summary> private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

/// <summary> /// 十二生肖 /// </summary> private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

/// <summary> /// 返回农历天干地支年 /// </summary> /// <param name="year">农历年</param> /// <returns></returns> public static string GetLunisolarYear(int year)
{
    if (year > 3)
    {
        int tgIndex = (year - 4) % 10;
        int dzIndex = (year - 4) % 12;

        return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "[", shengxiao[dzIndex], "]");

    }

    throw new ArgumentOutOfRangeException("无效的年份!");
}       

#endregion

#region 农历月

/// <summary> /// 农历月 /// </summary> private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };

/// <summary> /// 返回农历月 /// </summary> /// <param name="month">月份</param> /// <returns></returns> public static string GetLunisolarMonth(int month)
{
    if (month < 13 && month > 0)
    {
        return months[month - 1];
    }

    throw new ArgumentOutOfRangeException("无效的月份!");
}

#endregion

#region 农历日

/// <summary> /// /// </summary> private static string[] days1 = { "初", "十", "廿", "三" };

/// <summary> /// /// </summary> private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };

/// <summary> /// 返回农历日 /// </summary> /// <param name="day"></param> /// <returns></returns> 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("无效的日!");
}

#endregion

还有一个根据日期获取生肖的代码:

/// <summary> /// 返回生肖 /// </summary> /// <param name="datetime">公历日期</param> /// <returns></returns> public static string GetShengXiao(DateTime datetime)
{
    return shengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime)) - 1];
}
 source:http://www.cnblogs.com/huxj/archive/2010/03/04/1678160.html

阅读全文
类别: Net  查看评论

你可能感兴趣的:(C#)