C++实现日期类函数
来源:互联网 发布:程序员 月薪3万以下 编辑:程序博客网 时间:2024/05/17 03:09
今天做了一个项目,其中涉及到了日期的处理,通过一些查找,和自己的修改,写了一个工具类,给大家共享一下,有需要的可以直接用啦,每个方法我都基本
上写了注释,希望大家能好好的理解,共同进步吧。
下边就把代码贴出来了。
/***头文件。只进行了一些定义 Date.hauthor: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/29author: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;}*/
- 日期减少一天的C函数实现
- C++实现日期类函数
- C++:日期类的实现
- c++::实现一个日期类
- 【c++】日期类的实现
- c#日期函数详解
- C---格式化日期函数
- Date日期类的实现(C++)
- C语言中的日期函数
- C 获取编译日期函数
- C语言中的日期函数
- c/c++ 时间日期函数
- 【C++】日期类+日期万年历+日期计算器
- Linux常用C函数-日期时间函数
- [C/C++]标准C++ 时间日期函数
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- C语言实现当前日期时间函数NOW---国内习惯格式
- c语言之date_format日期格式化和strptime函数windows平台上实现
- 让xp远程桌面连接支持多用户与远程桌面默认端口修改
- CSS样式定义的优先级顺序总结
- 判断合法出栈序列
- 机制 策略
- 【iQuickTest自动化测试培训开启】
- C++实现日期类函数
- 表达式之谜总结
- QQ右下角通知窗口实现代码
- VirtualBox下Ubuntu与Windows之间的共享
- python 学习笔记(摘自《Python基础教程第2版》)
- Java程序实现系统托盘菜单
- 逻辑推理
- 初学spring+struts2+hibernate之配置篇
- oracle 小函数整理