公农历转换

来源:互联网 发布:php程序员发展方向 编辑:程序博客网 时间:2024/06/10 21:54

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace CSharp
{
    class calendar
    {
        private static ChineseLunisolarCalendar chinesedate = new ChineseLunisolarCalendar();

        public static void Main(String[] args)
        {
            Console.WriteLine("请选择您需要的功能!");
            Console.WriteLine("1.公历转农历 /n2.农历转公历");
            int label = int.Parse(Console.ReadLine());
            Console.WriteLine("依次输入年、月、日。");
            int Year = int.Parse(Console.ReadLine());
            int Month = int.Parse(Console.ReadLine());
            int Day = int.Parse(Console.ReadLine());
            ShowCurrentInfo(Year, Month, Day, label);
        }

        /// <summary>
        /// 输入日期,转换日期
        /// </summary>
        /// <param name="year">需要转换的年份</param>
        /// <param name="month">需要转换的月份</param>
        /// <param name="day">需要转换的日</param>
        public static void ShowCurrentInfo(int year, int month, int day, int label)
        {
            if (label == 1)
            {
                int leapmonth = chinesedate.GetLeapMonth(year);//获取第几个月是闰月
                DateTime dt = new DateTime(year, month, day);
                int IYear = chinesedate.GetYear(dt);
                int IMonth = chinesedate.GetMonth(dt);
                int IDay = chinesedate.GetDayOfMonth(dt);

                if (leapmonth > 0)//如果今年存在闰月
                {
                    if (month == leapmonth)//闰月为当前月份
                    {
                        Console.WriteLine("农历日期:{0}年{1}月{2}日。", IYear, IMonth - 1, IDay);
                    }
                    else
                    {
                        Console.WriteLine("农历日期:{0}年{1}月{2}日。", IYear, IMonth, IDay);
                    }
                }
                else
                {
                    Console.WriteLine("农历日期:{0}年{1}月{2}日。", IYear, IMonth, IDay);
                }
            }
            else if (label == 2)
            {
                DateTime dt = chinesedate.ToDateTime(year, month, day, 0, 0, 0, 0);
                Console.WriteLine("公历日期:" + dt.Date.ToString("yyyy年MM月d日"));
            }
            else
            {
                Console.WriteLine("输入有误!");
            }
        }
    }
}

原创粉丝点击