我开发的一个时间运算的类v1.0

//说明:时间运算,经常在程序中遇到,这次开发的类解决了时间加减的问题,在一个时间点,增加或者减少时间的某个部分,得到一个新的时间,类完整代码如下:

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

namespace CalcNC
{
    class CalcDateTime
    {
        private int second;
        public int Second
        {
            get
            {
                return second;
            }
            set
            {
                second = value;
            }
        }
        private int minute;
        public int Minute
        {
            get
            {
                return minute;
            }
            set
            {
                minute = value;
            }
        }
        private int hour;
        public int Hour
        {
            get
            {
                return hour;
            }
            set
            {
                hour = value;
            }
        }
        private int day;
        public int Day
        {
            get
            {
                return day;
            }
            set
            {
                day = value;
            }
        }
        private int month;
        public int Month
        {
            get
            {
                return month;
            }
            set
            {
                month = value;
            }
        }
        private int year;
        public int Year
        {
            get
            {
                return year;
            }
            set
            {
                Year = value;
            }
        }
        public CalcDateTime()
        {
            second = DateTime.Now.Second;
            minute = DateTime.Now.Minute;
            hour = DateTime.Now.Hour;
            day = DateTime.Now.Day;
            month = DateTime.Now.Month;
            year = DateTime.Now.Year;
        }
        public CalcDateTime(int second, int minute, int hour, int day, int month, int year)
        {
            this.second = second;
            this.minute = minute;
            this.hour = hour;
            this.day = day;
            this.month = month;
            this.year = year;
        }
        //CalcDateTime(int day, int month, int year)
        //{
        //    second = DateTime.Now.Second;
        //    minute = DateTime.Now.Minute;
        //    hour = DateTime.Now.Hour;
        //    this.day = day;
        //    this.month = month;
        //    this.year = year;
        //}
        public void addSecond(int second)
        {
            int Tmp = this.second + second; ;
    &nbsp ;       if (second > 0)
            {
                while (Tmp >= 60)
                {
                    Tmp -= 60;
                    addMinute(1);
                }
            }
            else
            {
                while (Tmp < 0)
                {
                    Tmp += 60;
                    addMinute(-1);
                }
            }
            this.second = Tmp;
        }
        public void addMinute(int minute)
        {
            int tmp = this.minute + minute;
            if (minute > 0)
            {
                while (tmp >= 60)
                {
                    tmp -= 60;
                    addHour(1);
                }
            }
            else
            {
                while (tmp < 0)
                {
                    tmp += 60;
                    addHour(-1);
                }
            }
            this.minute = tmp;
        }
        public void addHour(int hour)
        {
            int Tmp = this.hour + hour;
            if (hour > 0)
            {
                while (Tmp >= 24)
                {
                    Tmp -= 24;
                    addDay(1);
                }
            }
            else
            {
                while (Tmp < 0)
                {
                    Tmp += 24;
                    addDay(-1);
                }
            }
            this.hour = Tmp;
        }
        public void addDay(int day)
        {
            int tmp = this.day + day;
            if (day > 0)
            {
                while (tmp > GetMaxDayiOfMonth(this.year, this.month))
                {
                    tmp = tmp - GetMaxDayiOfMonth(this.year, this.month);
                    addMonth(1);
                }
            }
            else
            {
                while (tmp <= 0)
                {
                    tmp = tmp + GetMaxDayiOfMonth(this.year, this.month - 1);
                    addMonth(-1);
                }
            }
            this.day = tmp;
        }
        public void addMonth(int month)
        {
            int tmp = this.month + month;
            if (month > 0)
            {
                while (tmp > 12)
                {
                    tmp -= 12;
                    addYear(1);
                }
            }
            else
            {
                while (tmp < 0)
                {
                    tmp += 12;
                    addYear(-1);
                }
            }
            this.month = tmp;
            if (this.day > GetMaxDayiOfMonth(this.year,this.month))
                this.day = GetMaxDayiOfMonth(this.year, this.month);
        }
        public void addYear(int year)
        {
            this.year += year;
        }
        public override string ToString()
        {
            string result = year.ToString() + "年" + Format(month) + "月" + Format(day) + "日 ";
            result += Format(hour) + ":" + Format(minute) + ":" + Format(second);
            return result;
        }
        public string Format(int ori)
        {
            ori = Math.Abs(ori);
            if (ori < 10)
            {
                return "0" + ori.ToString();
            }
            else
            {
                return ori.ToString();
            }
        }
        public int GetMaxDayiOfMonth(int y, int m)
        {
            int day = 0;
            if (m == 2)
            {
                if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
                {
                    day=29;
                }
                day=28;
            }
            else
            {
                int[] month1 = new int[7] { 1, 3, 5, 7, 8, 10, 12 };
                int[] month2 = new int[4] { 4, 6, 9, 11 };
                for (int i = 0; i < month1.Length; i++)
                {
                    if (m == month1[i])
                        day = 31;
                }
                for (int i = 0; i < month2.Length; i++)
                {
                    if (m == month2[i])
                    {
                        day = 30;
                    }
                }
            }
            return day;
        }
    }
}

posted @ 2010-11-28 19:14  crid  阅读(112)  评论(0编辑  收藏  举报