C#日期类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
 二、请设计日期类。要求:
    1、可以初始化日期    
    2、可以重新设置日期
    3、提供年、月后获取这个月的天数,即这个月的最后一天是几号   .GetMonthDays(int y,int m)
    4、提供年、月、日后获取这一天为当年的第几天              .DayInYear(int y, int m, int d)
    5、含以下方法,加一个天数获取另一个日期,比如 2015-10-27 加100 天后获取另一个日期  .Getdate(int n)
    6、含以下方法,获取两个日期之间相差的天数,比如2016-3-29与 2008-8-8 相差多少天   .Judgeday(Date t1, Date t2)
    7、获取某个日期是星期几。 .xingqi(Date v)
    检查网址::  http://bjtime.cn/riqi.asp
 */
namespace homework2
{

    class Date
    {
        private int year, month, day;
        public Date()
        {
            year = month = day = 0;
        }
        public Date(int y,int m,int d)
        {
            year = y; month = m; day = d;
        }
        public void ResetDate(int y, int m, int d)
        {
            year = y; month = m; day = d;
        }
        public void Disp()
        {
            Console.Write("{0},{1},{2}", year, month, day);
        }
        public void Dispn()
        {
            Console.WriteLine("{0},{1},{2}", year, month, day);
        }
        bool IsLeap(int year)// 判断是否是闰年
        {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                return true;
            else
                return false;
        }
        public int GetMonthDays()
        {
            if (month == 2)
            {
                if (IsLeap(year))
                {
                    return 29;
                }
                else
                    return 28;    
            }
            if (month<8)
            {
                if (month%2 != 0)
                    return 31;
                else 
                    return 30;
            }
            else 
            {
                if (month % 2 != 0)
                    return 30;
                else
                    return 31;
            }
        }//提供年、月后获取这个月的天数,即这个月的最后一天是几号
        public int DayInYear()// 判断是今年的第几天
        {
            int result = 0;
            switch (month)
            {
                case 1:
                    result += 0;
                    break;
                case 2:
                    result += 31;
                    break;
                case 3:
                    result += 31 + 28;
                    break;
                case 4:
                    result += 31 * 2 + 28;
                    break;
                case 5:
                    result += 31 * 2 + 30 + 28;
                    break;
                case 6:
                    result += 31 * 3 + 30 + 28;
                    break;
                case 7:
                    result += 31 * 3 + 30 * 2 + 28;
                    break;
                case 8:
                    result += 31 * 4 + 30 * 2 + 28;
                    break;
                case 9:
                    result += 31 * 5 + 30 * 2 + 28;
                    break;
                case 10:
                    result += 31 * 5 + 30 * 3 + 28;
                    break;
                case 11:
                    result += 31 * 6 + 30 * 3 + 28;
                    break;
                case 12:
                    result += 31 * 6 + 30 * 4 + 28;
                    break;
                default:
                    Console.WriteLine("输入的月份非法,不在1-12之内!");
                    break;
            }
            if (IsLeap(year))
            {
                if (month >= 3)
                    result += 1;
            }
            return result + day;
        }
        public void Swap(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = temp;
        }
        public int Myabs(int a)
        {
            if (a <= 0)
                return (-a);
            else
                return a;
        }//绝对值
        public Date Getdate(int n)
        {
            Date t = this;
            int []a = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            n += t.day;
            do
            {
                if (IsLeap(t.year))
                    a[2] += 1;
                n -= a[t.month];
                if (++t.month > 12)
                {
                    t.month = 1;
                    t.year++;
                }
            } while (n > a[t.month]);
            t.day = n;
            return t;
        }//加一个天数获取另一个日期

        public int xingqi()
        {
            int W = 0;
            switch (month)
            {
                case 1:
                    year -= 1;
                    month = 13;
                    break;
                case 2:
                    year -= 1;
                    month = 14;
                    break;
            }
            W = (day + 2 * month + 3 * (month + 1) / 5 + year 
                + year / 4 - year / 100 + year / 400) % 7;
            return W+1;
        } // 某个日期是星期几


        public int Judgeday(Date t2)
        {
            if (this.year >= t2.year) // 年份小的是y1
            {
                Swap(ref this.year, ref t2.year);
                Swap(ref this.month, ref t2.month);
                Swap(ref this.day, ref t2.day);
            }
            if (this.year == t2.year)
            {
                return Math.Abs(this.DayInYear() // 年份一样,月份不同
                                - t2.DayInYear());
            }
            else  // 年份不一样
            {
                int days = 0;
                days = 365 - this.DayInYear()
                       + t2.DayInYear();
                days += 366 * GetLeapNum(this.year, t2.year);
                days += 365 * ((t2.year - this.year) - GetLeapNum(this.year, t2.year) - 1);
                if (IsLeap(this.year))
                {
                    days += 1;
                }
                return days;
            }
        }//两个日期相差天数
        public int GetLeapNum(int y1, int y2)
        {
            if (y1 >= y2)
            {
                Swap(ref y1, ref y2);
            }
            int num = 0;
            for (int i = y1 + 1; i < y2; i++)
            {
                if (IsLeap(i))
                    num++;
            }
            return num;
        }//返回从y1和y2之间有几个闰年,不包括y1,y2
        ~Date()     //析构函数
        {
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Date t1 = new Date(2008, 2, 8);
            t1.Dispn();
            Console.WriteLine("这个月最后一天是{0}号",t1.GetMonthDays());
            Console.WriteLine("这一天是当年的第{0}天\n", t1.DayInYear());

            int n = 100;
            Date t2 = new Date(2015, 10, 27);
            t2.Disp();
            Console.WriteLine("加上{0}天的日期是:", n);
            t2 = t2.Getdate(n);
            t2.Dispn();
            Console.WriteLine();

            t1.ResetDate(2008, 8, 8);
            t2.ResetDate(2016, 3, 29);
            t1.Disp(); Console.Write(" 与 "); t2.Disp();
            Console.WriteLine("  相差{0}天\n", t1.Judgeday(t2));

            t1.Disp();
            Console.WriteLine("为星期{0}",t1.xingqi());

            Console.ReadKey();



        }
    }

}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值