#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)   //构造
    :_year(year)
    , _month(month)
    , _day(day)
    {
        if (!isInvalidDate(_year, _month, _day))
        {
            _year = 1900;
            _month = 1;
            _day = 1;
        }
    }
    
    Date operator+(int count)
    {
        Date tmp(*this);
        tmp._day += count;
        ToCorrect(tmp);
        return tmp;
    }
    Date operator-(int count)
    {
        Date tmp(*this);
        tmp._day -= count;
        ToCorrect(tmp);
        return tmp;
    }
    
    Date& operator++()   //前置++
    {
        (*this)++;
        return *this;
    }
    Date operator++(int)    //后置++
    {
        Date tmp(*this);
        (*this)+=1;
        return tmp;
    }
    Date& operator--()
    {
        (*this)--;
        return *this;
    }
    Date operator--(int)
    {
        Date tmp(*this);
        (*this)--;
        return tmp;
    }
    int operator-(const Date &d)
    {
        int flag = 1;
        Date max = *this;
        Date min = d;
        if (*this<d)
        {
            max = d;
            min = *this;
            flag = -1;
        }
        int count=0;
        while (min != max)
        {
            ++min;
            count++;
        }
        return count*flag;
    }
    Date& operator+=(int day)
    {
        *this = *this + day;
        return *this;
    }
    bool operator!=(const Date &d)
    {
        return !(*this == d);
    }
    bool operator<(const Date &d)
    {
        return !((*this>d)||(*this==d));
    }
    bool operator>=(const Date &d)
    {
        return !(*this<d);
    }
    bool operator>(const Date &d)
    {
        return (_year > d._year
            || (_year == d._year && _month > d._month)
            || (_year == d._year && _month == d._month && _day > d._day));
    }
    bool operator==(const Date &d)
    {
        return ((_year == d._year) && (_month == d._month) && (_day == d._day));
    }
    
public:    
    bool IsLeapYear(int year)
    {
        if(year % 400 || (year % 4 && year % 100))
            return true;
        return false;
    }
    int YearsOfMonth(int year, int month)
    {
        int day;
        int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        day = days[month];
        if (month == 2)
            day += IsLeapYear(year);
        return day;
    }
    
    Date ToCorrect(Date &d)
    {
        if (d._day>YearsOfMonth(d._year, d._month))
        {
            while (d._day > YearsOfMonth(d._year, d._month))
            {
                 d._day -= YearsOfMonth(d._year,d._month);

if (d._month == 12)
                {
                    d._year++;
                    d._month = 1;
                }
                else
                {
                    ++d._month;
                }
            }
        }
        else
        {
            while (d._day <= 0)
            {
                if (d._month == 1)
                {
                    d._year--;
                    d._month = 12;
                }
                else
                {
                    --d._month;
                }
                d._day += YearsOfMonth(d._year, d._month);
            }
        }
        return d;
    }

bool isInvalidDate(int year, int month, int day)
    {
        if ((year < 1) || (month<0 || month>12) || (day<0 || day>YearsOfMonth(year,month)))
            return false;
        return true;
    }
    void Display()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    friend istream& operator>>(istream& is, Date &d);
    friend ostream& operator<<(ostream& os, const Date &d);
private:
    int _year;
    int _month;
    int _day;
};
istream& operator>>(istream& is, Date& d)
{
    cout << "请输入一个日期" << endl;
    is >> d._year >> d._month >> d._day;
    return is;
}

ostream& operator<<(ostream& os, const Date &d)
{
    cout << d._year << "-" <<d. _month << "-" << d._day << endl;
    return os;
}
int main()
{
    /*Date d1(2016,8,18);
    //d1.Display();

//d1 = d1++;
    cout << d1 << endl;*/

//Date d1(2015, 12, 3);
    //(d1++).Display();  //d1.operator++(&d1, 0);
    //(++d1).Display(); //d1.operator++(&d1);

Date d1(2015, 12, 3);
    Date d2(2015, 11, 1);
    cout << (d1 - d2) << endl;

//Date d1(2015, 12, 3);
    //Date ret = d1 + 40; //operator+
    //ret.Display();

/*Date d1(2015, 12, 3);
    Date ret = d1 + 40;
    d1 = ret;
    ret = d1 - 40;
    ret.Display();*/
    
    /*Date ret;
    Date d2(2015, 1, 1);
    ret = d2 - 1;
    ret.Display();*/
    return 0;
}

C++实现日期类(Date类)的更多相关文章

  1. 日期操作类--Date类

    Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...

  2. Java关于时间日期的Date类和Calendar类概述

    1.      System.currentTimeMillis()方法 可以获取当前时间距离1970年01月01日00时00分00秒的秒数,如果程序运行在北京时区,则获取的数据是当前时间距离1970 ...

  3. java日期时间Date类

    java.util包提供了Date类来封装当前的日期和时间. Date类提供两个构造函数来实例化Date对象. 第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函数接收一个参 ...

  4. java常用类————Date类

    Date类在Java.util包中. 一.功能介绍:创建Date对象,获取时间,格式化输出的时间. 二.对象创建:1.使用Date类无参数的构造方法创建的对象可以获取本地时间.例如: Date now ...

  5. Java学习关于时间操作的应用类--Date类、Calendar类及其子类

    Date类 Date类封装了当期时间和日期.与Java1.0定义的原始版的Date类相比,Date类发生了本质的变化.在Java1.1发布时,原始版Date类定义的许多功能被移进Calendar类和D ...

  6. Java api 入门教程 之 JAVA的Date类与Calendar类

    在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理. 一.这里简单介绍一下Date类的使 ...

  7. JAVA的Date类与Calendar类【转】

    Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...

  8. Java的Date类与Calendar类

    一:Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Da ...

  9. Date 类 02

    Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...

  10. Java知多少(77)日期和时间类

    Java 的日期和时间类位于 java.util 包中.利用日期时间类提供的方法,可以获取当前的日期和时间,创建日期和时间参数,计算和比较时间. Date 类 Date 类是 Java 中的日期时间类 ...

随机推荐

  1. Android fill_parent、wrap_content和match_parent的区别

    三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便. 1)fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以 ...

  2. 开源搜索引擎Solr的快速搭建及集成到企业门户最佳实施方案--转载

    笔者经过研究查阅solr官方相关资料经过两周的研究实现了毫秒级百万数据的搜索引擎的搭建并引入到企业门户.现将实施心得和步骤分享一下. 1.      jdk1.6 安装jdk1.6到系统默认目录下X: ...

  3. cf 383 D

    D. Antimatter time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. 2014多校第六场 1010 || HDU 4930 Fighting the Landlords (模拟)

    题目链接 题意 : 玩斗地主,出一把,只要你这一把对方要不了或者你出这一把之后手里没牌了就算你赢. 思路 : 一开始看了第一段以为要出很多次,实际上只问了第一次你能不能赢或者能不能把牌出尽. #inc ...

  5. 机器学习之神经网络模型-上(Neural Networks: Representation)

    在这篇文章中,我们一起来讨论一种叫作"神经网络"(Neural Network)的机器学习算法,这也是我硕士阶段的研究方向.我们将首先讨论神经网络的表层结构,在之后再具体讨论神经网 ...

  6. __stdcall 与 __cdecl

    (1) _stdcall调用 _stdcall是Pascal程序的缺省调用方式,参数采用从右到左的压栈方式,被调函数自身在返回前清空堆栈. WIN32 Api都采用_stdcall调用方式,这样的宏定 ...

  7. CentOS系统安装tomcat

    按照以下步骤操作即可wget http://mirrors.noc.im/apache/tomcat/tomcat-9/v9.0.0.M6/bin/apache-tomcat-9.0.0.M6.tar ...

  8. jq的bind用法

    type,[data],function(eventObject)String,Object,Function type: 含有一个或多个事件类型的字符串,由空格分隔多个事件.比如"clic ...

  9. 如何用crontab运行一个图形化界面的程序

    crontab是linux中定时任务的 执行crontab -e可以编辑定时列表(export DISPLAY=:0 指定显示器或者export DISPLAY=localhost:0) 15 13 ...

  10. 如何在Java客户端调用RESTful服务

    在这个例子中,我们将看到如何使用java.net包实用工具,创建一个访问REST服务RESTful的客户端.当然这不是创建一个RESTful客户端最简单的方法,因为你必须自己读取服务器端的响应,以及J ...