Qt常用类 - QDate
1 开发环境
在介绍内容之前,先说明一下开发环境,如下图:
Qt版本:Qt5.3.2;
Qt开发工具:Qt Creater 3.2.1;
Qt构建工具:Desktop Qt 5.3 MinGW 32bit;
Qt开发平台:Windows 7 64bit。
2 QDate
QDate是Qt中用到的日期类,可以用于对自然日期的记录和运算。首先,将QDate中的常用接口整理出来,分为初始化、设置和获取、计算、转换为字符串、比较、静态接口中的转换字符串共6类,这样方便学习和理解。
//初始化
QDate(); //构造一个NULL对象
QDate(int y, int m, int d); //
static QDate QDate::currentDate(); //返回系统时间
static QDate QDate::fromString(const QString &string, Qt::DateFormat format = Qt::TextDate);
static QDate QDate::fromString(const QString &string, const QString &format);
bool isNull();
bool isValid();
static bool QDate::isValid(int year, int month, int day);
//设置和获取
bool setDate(int year, int month, int day);
int day() const;
int month() const;
int year() const;
void getDate(int *year, int *month, int *day);
//计算
int dayOfWeek() const;
int dayOfYear() const;
int daysInMonth() const;
int daysInYear() const;
qint64 daysTo(const QDate &d) const; //不包含参数'd'的这天
int weekNumber(int *yearNumber = 0) const;
QDate addDays(qint64 ndays) const;
QDate addMonths(int nmonths) const;
QDate addYears(int nyears) const;
//转换为字符串
QString toString(const QString &format) const;
QString toString(Qt::DateFormat = Qt::TextDate) const;
//比较
bool operator==(QDate &d) const;
bool operator!=(QDate &d) const;
bool operator< (QDate &d) const;
bool operator<=(QDate &d) const;
bool operator> (QDate &d) const;
bool operator>=(QDate &d) const;
//静态接口中的转换字符串
static QString QDate::longDayName(int weekday, MonthNameType type = DateFormat);
static QString QDate::longMonthName(int month, MonthNameType type = DateFormat);
static QString QDate::shortDayName(int weekday, MonthNameType type = DateFormat);
static QString QDate::shortMonthName(int month, MonthNameType type = DateFormat);
下面,是上述接口的使用例程。
#include <QDate>
#define qPrintableU8(_string) (QString(_string).toUtf8().constData())
#define DATE_STRING_FORMAT ("yyyy-MM-dd")
//QDate();
//QDate(int y, int m, int d);
//static QDate QDate::currentDate();
//static QDate QDate::fromString(const QString &string, Qt::DateFormat format = Qt::TextDate);
//static QDate QDate::fromString(const QString &string, const QString &format);
//bool isNull();
//bool isValid();
//static bool QDate::isValid(int year, int month, int day);
QDate dateNull;
qDebug("dateNull: %d-%d-%d.", dateNull.year(), dateNull.month(), dateNull.day());
qDebug("dateNull is null? %s.", dateNull.isNull() ? "yes" : "no");
qDebug("dateNull is valid? %s.", dateNull.isValid() ? "yes" : "no");
qDebug("dateNull is invalid? %s.", !QDate::isValid(dateNull.year(), dateNull.month(), dateNull.day()) ? "yes" : "no");
QDate dateContruct(2000, 1, 1);
qDebug("dateContruct: %04d-%02d-%02d.", dateContruct.year(), dateContruct.month(), dateContruct.day());
QDate dateSystem = QDate::currentDate();
qDebug("dataSystem: %04d-%02d-%02d.", dateSystem.year(), dateSystem.month(), dateSystem.day());
QDate dateFs1 = QDate::fromString("2018-07-20", Qt::ISODate);
qDebug("dateFs1: %04d-%02d-%02d.", dateFs1.year(), dateFs1.month(), dateFs1.day());
QDate dateFs2 = QDate::fromString("2011-01-01", "yyyy-MM-dd");
qDebug("dateFs2: %04d-%02d-%02d.", dateFs2.year(), dateFs2.month(), dateFs2.day());
QDate dateFs3 = QDate::fromString("20120202", "yyyyMMdd");
qDebug("dateFs3: %04d-%02d-%02d.", dateFs3.year(), dateFs3.month(), dateFs3.day());
QDate dateFs4 = QDate::fromString("13.3.3", "yy.M.d");
qDebug("dateFs4: %04d-%02d-%02d.", dateFs4.year(), dateFs4.month(), dateFs4.day());
//bool setDate(int year, int month, int day);
//int day() const;
//int month() const;
//int year() const;
//void getDate(int *year, int *month, int *day);
QDate dateSet;
dateSet.setDate(1999, 12, 3);
int year = 0, month = 0, day = 0;
dateSet.getDate(&year, &month, &day);
qDebug("getDate: %04d-%02d-%02d.", year, month, day);
//int dayOfWeek() const;
//int dayOfYear() const;
//int daysInMonth() const;
//int daysInYear() const;
//qint64 daysTo(const QDate &d) const;
//int weekNumber(int *yearNumber = 0) const;
//QDate addDays(qint64 ndays) const;
//QDate addMonths(int nmonths) const;
//QDate addYears(int nyears) const;
qDebug("DateSystem, dayOfWeek = %d.", dateSystem.dayOfWeek());
qDebug("DateSystem, dayOfYear = %d.", dateSystem.dayOfYear());
qDebug("DateSystem, daysInMonth = %d.", dateSystem.daysInMonth());
qDebug("DateSystem, daysInYear = %d.", dateSystem.daysInYear());
qDebug("DateSystem, daysTo20181231 = %lld.", dateSystem.daysTo(QDate(2018, 12, 31)));
qDebug("DateSystem, weekNumber = %d.", dateSystem.weekNumber());
QDate dateAfter2Days = dateSystem.addDays(2);
qDebug("DateSystem, addDays(2): %04d-%02d-%02d.", dateAfter2Days.year(), dateAfter2Days.month(), dateAfter2Days.day());
QDate dateAfter2Months = dateSystem.addMonths(2);
qDebug("DateSystem, addMonths(2): %04d-%02d-%02d.", dateAfter2Months.year(), dateAfter2Months.month(), dateAfter2Months.day());
QDate dateAfter2Years = dateSystem.addYears(2);
qDebug("DateSystem, addYears(2): %04d-%02d-%02d.", dateAfter2Years.year(), dateAfter2Years.month(), dateAfter2Years.day());
//QString toString(const QString &format) const;
//QString toString(Qt::DateFormat = Qt::TextDate) const;
QString strDate1 = dateSystem.toString("yyyy.MM.dd");
qDebug("strDate1 = %s.", qPrintableU8(strDate1));
QString strDate2 = dateSystem.toString(Qt::ISODate);
qDebug("strDate2 = %s.", qPrintableU8(strDate2));
//bool operator==(QDate &d) const;
//bool operator!=(QDate &d) const;
//bool operator< (QDate &d) const;
//bool operator<=(QDate &d) const;
//bool operator> (QDate &d) const;
//bool operator>=(QDate &d) const;
QDate dateEarlier(2018, 12, 31), dateLater(2019, 12, 31);
qDebug("dateEarlier == dateLater? %s.", dateEarlier == dateLater ? "√" : "×");
qDebug("dateEarlier != dateLater? %s.", dateEarlier != dateLater ? "√" : "×");
qDebug("dateEarlier < dateLater? %s.", dateEarlier < dateLater ? "√" : "×");
qDebug("dateEarlier <= dateLater? %s.", dateEarlier <= dateLater ? "√" : "×");
qDebug("dateEarlier > dateLater? %s.", dateEarlier > dateLater ? "√" : "×");
qDebug("dateEarlier >= dateLater? %s.", dateEarlier >= dateLater ? "√" : "×");
//static QString QDate::longDayName(int weekday, MonthNameType type = DateFormat);
//static QString QDate::longMonthName(int month, MonthNameType type = DateFormat);
//static QString QDate::shortDayName(int weekday, MonthNameType type = DateFormat);
//static QString QDate::shortMonthName(int month, MonthNameType type = DateFormat);
QString strLongDayNameDf = QDate::longDayName(dateSystem.dayOfWeek(), QDate::DateFormat);
QString strLongDayNameAsf = QDate::longDayName(dateSystem.dayOfWeek(), QDate::StandaloneFormat);
qDebug("strLongDayNameDf = %s, strLongDayNameAsf = %s.", qPrintableU8(strLongDayNameDf), qPrintableU8(strLongDayNameAsf));
QString strShortDayNameDf = QDate::shortDayName(dateSystem.dayOfWeek(), QDate::DateFormat);
QString strShortDayNameAsf = QDate::shortDayName(dateSystem.dayOfWeek(), QDate::StandaloneFormat);
qDebug("strShortDayNameDf = %s, strShortDayNameAsf = %s.", qPrintableU8(strShortDayNameDf), qPrintableU8(strShortDayNameAsf));
QString strLongMonthNameDf = QDate::longMonthName(dateSystem.month(), QDate::DateFormat);
QString strLongMonthNameAsf = QDate::longMonthName(dateSystem.month(), QDate::StandaloneFormat);
qDebug("strLongMonthNameDf = %s, strLongMonthNameAsf = %s.", qPrintableU8(strLongMonthNameDf), qPrintableU8(strLongMonthNameAsf));
QString strShortMonthNameDf = QDate::shortMonthName(dateSystem.month(), QDate::DateFormat);
QString strShortMonthNameAsf = QDate::shortMonthName(dateSystem.month(), QDate::StandaloneFormat);
qDebug("strShortMonthNameDf = %s, strShortMonthNameAsf = %s.", qPrintableU8(strShortMonthNameDf), qPrintableU8(strShortMonthNameAsf));
在学习过程中,可以将上述代码放到一个按钮的响应函数中,以调试方式运行,就可以在Qt Creater中的应用程序输出窗口看到输出结果了。