今天做了一个项目,其中涉及到了日期的处理,通过一些查找,和自己的修改,写了一个工具类,给大家共享一下,有需要的可以直接用啦,每个方法我都基本
上写了注释,希望大家能好好的理解,共同进步吧。
下边就把代码贴出来了。
/***头文件。
只进行了一些定义 Date.h
author:wallwind time 2011/8/29
**/
#pragma once
#include <string>
#include <iostream>
class Date //创建一个Date类,
{
int year,month,day;//
private:
int DayOfMonth(int y,int m)const;//返回一个月的天数。
int ToInt()const;//
public:
Date() //构造函数,初始化默认的日期
:year(1900),month(1),day(1)
{
}
Date(int y,int m,int d)
:year(y),month(m),day(d)//对日期赋值,判断有效的日期。
{
if((y<=0)||(m<=0 || m>12)||(d<=0 || d>DayOfMonth(y,m)))
{
year = 1900;
month = day = 1;
}
}
virtual ~Date()//虚析构函数。
{
}
int GetYear()const//返回年份
{
return year;
}
int GetMonth()const //返回月份
{
return month;
}
int GetDay()const //返回日期
{
return day;
}
bool IsLeapyear() const //判断是否为平年。
{
return year%400?(year%100?(year%4?(false):(true)):(false)):true;
}
bool IsLeapyear(const int y) const //判断是否为平年。
{
return y%400?(y%100?(y%4?false:true):false):true;
}
void Display() const //输出日期值
{
std::cout<<year<<"-"<<month<<"-"<<day<<std::endl;
}
//重载操作符
friend Date operator + (const int v,const Date a);
friend Date operator + (const Date a,const int v);
friend Date operator +=(Date &a,const int v);
friend Date operator ++(Date &a);
friend Date operator ++(Date &a,int);
friend Date operator - (const Date a,const int v);
friend int operator - (const Date a,const Date b);
friend Date operator -=(Date &a,const int v);
friend Date operator --(Date &a);
friend Date operator --(Date &a,int);
friend bool operator > (const Date a,const Date b);
friend bool operator >=(const Date a,const Date b);
friend bool operator < (const Date a,const Date b);
friend bool operator <=(const Date a,const Date b);
friend bool operator ==(const Date a,const Date b);
friend bool operator !=(const Date a,const Date b);
//friend std::ostream & operator <<(std::ostream &os,const Date &d);
//friend std::istream & operator >>(std::istream &is,Date &d);
};
下面是实现cpp文件
/***
Date 日期类实现。//Date.cpp 2011/8/29
author:wallwind time 2011/8/29
***/
#include "Date.h"
//得到每个月的天数。。。。
int Date::DayOfMonth(int y,int m)const
{
int d = 0;
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
d = 31;
break;
case 4:
case 6:
case 9:
case 11:
d = 30;
break;
case 2:
d = 28 + IsLeapyear(y);
break;
}
return d;
}
int Date::ToInt()const
{
int sum =0;
for(int y=1;y<year;++y)
{
sum += 365+IsLeapyear(y);
}
for(int m=1;m<month;++m)
sum += DayOfMonth(year,m);
sum += day;
return sum;
}
/日期加天数。。const int v,const Date a。。
Date operator + (const int v,const Date a)
{
Date d = a;
if(v==0) return d;//如果天数为0,返回当个月
if(v>0)//如果大于零。
{
d.day += v; 将当天日期加上要增加的数
while(d.day>d.DayOfMonth(d.year,d.month))//当加所加天数超过当月天数
{
d.day -= d.DayOfMonth(d.year,d.month);
d.month++; //月份加一
if(d.month>12) ///如果月份大于12,那么就增加一年。
{
++d.year;
d.month = 1;
}
}
return d;
}
else
return d-(-v);
}
/日期加天数。。const Date a,const int v 同上,方法的重载。
Date operator +(const Date a,const int v)
{
Date d = a;
if(v==0) return d;
if(v>0)
{
d.day += v;
while(d.day>d.DayOfMonth(d.year,d.month))
{
d.day -= d.DayOfMonth(d.year,d.month);
d.month++;
if(d.month>12)
{
++d.year;
d.month = 1;
}
}
return d;
}
else
return d-(-v);
}
//加操作~同上。
Date operator +=(Date &a,const int v)
{
if(v==0) return a;
if(v>0)
{
a.day+=v;
while(a.day>a.DayOfMonth(a.year,a.month))
{
a.day -= a.DayOfMonth(a.year,a.month);
a.month++;
if(a.month>12)
{
++a.year;
a.month = 1;
}
}
return a;
}
else
return a-=(-v);
}
//日期自增操作。。。。。。同上,,,,,
Date operator ++(Date &a)
{
++a.day;
if(a.day>a.DayOfMonth(a.year,a.month))
{
a.day-=a.DayOfMonth(a.year,a.month);
a.month++;
if(a.month>12)
{
++a.year;
a.month = 1;
}
}
return a;
}
自增操作。
Date operator ++(Date &a,int)
{
Date d = a;
++a.day;
if(a.day>a.DayOfMonth(a.year,a.month))
{
a.day-=a.DayOfMonth(a.year,a.month);
a.month++;
if(a.month>12)
{
++a.year;
a.month = 1;
}
}
return d;
}
/减操作。。。。。
Date operator - (const Date a,const int v)
{
Date d = a;
if(v==0) return d;
if(v>0)
{
d.day -= v;
while(d.day<=0)
{
--d.month;
if(d.month==0)
{
d.month=12;
--d.year;
}
d.day+=d.DayOfMonth(d.year,d.month);
}
return d;
}
else
return d+(-v);
}
int operator - (const Date a,const Date b)
{
return a.ToInt()-b.ToInt();
}
Date operator -=(Date &a,const int v)
{
if(v==0) return a;
if(v>0)
{
a.day -= v;
while(a.day<=0)
{
--a.month;
if(a.month==0)
{
a.month=12;
--a.year;
}
a.day+=a.DayOfMonth(a.year,a.month);
}
return a;
}
else
return a+=(-v);
}
Date operator --(Date &a)
{
--a.day;
while(a.day<=0)
{
--a.month;
if(a.month==0)
{
a.month = 12;
--a.year;
}
a.day += a.DayOfMonth(a.year,a.month);
}
return a;
}
Date operator --(Date &a,int)
{
Date d = a;
--a.day;
while(a.day<=0)
{
--a.month;
if(a.month==0)
{
a.month = 12;
--a.year;
}
a.day += a.DayOfMonth(a.year,a.month);
}
return d;
}
/日期的比较。。
bool operator > (const Date a,const Date b)
{
return a.ToInt()>b.ToInt();
}
bool operator >=(const Date a,const Date b)
{
return a.ToInt()>=b.ToInt();
}
bool operator < (const Date a,const Date b)
{
return a.ToInt()<b.ToInt();
}
bool operator <=(const Date a,const Date b)
{
return a.ToInt()<=b.ToInt();
}
bool operator ==(const Date a,const Date b)
{
return a.ToInt()==b.ToInt();
}
bool operator !=(const Date a,const Date b)
{
return a.ToInt()!=b.ToInt();
}
/*std::ostream & operator <<(std::ostream &os,const Date &d)
{
os<<d.year<<"-"<<d.month<<"-"<<d.day;
return os;
}
std::istream & operator >>(std::istream &is,Date &d)
{
Date old = d;
is>>d.year>>d.month>>d.day;
if((d.year<=0) ||(d.month>12||d.month<=0) || (d.day<=0||d.day>d.DayOfMonth(d.year,d.month)))
{
d = old;
throw std::exception("Invalid number of date.");
}
return is;
}
*/