参考:http://zetcode.com/gui/qt5/datetime/
Qt5用于QDate
, QTime
和 QDateTime
这三个类处理日期和时间。QDate
是处理公历中日期的方法,它有确定日期,比较或操纵日期的方法。QTime
是处理时间的方法,它提供了比较时间,确定时间和各种其他操纵时间的方法。QDateTime
是一个将QDate
和QTime
对象组合到一个对象中的类。
初始化时间和日期
日期和时间对象可以用两种基本方式初始化。在对象的构造函数中初始化它们,或者可以创建空对象并在以后用数据填充它们。
// init.cpp
#include <QTextStream>
#include <QDate>
#include <QTime>
int main(void) {
QTextStream out(stdout);
QDate dt1(2015, 4, 12); // 三个参数分别为年、月、日
out << "The date is " << dt1.toString() << endl;
QDate dt2;
dt2.setDate(2015, 3, 3);
out << "The date is " << dt2.toString() << endl;
QTime tm1(17, 30, 12, 55);
out << "The time is " << tm1.toString("hh:mm:ss.zzz") << endl;
QTime tm2; // 创建一个空的QTime对象
tm2.setHMS(13, 52, 45, 155); // 四个参数分别为时、分、秒、毫秒
out << "The time is " << tm2.toString("hh:mm:ss.zzz") << endl;
}
out << "The date is " << dt1.toString() << endl;
通过setHMS()
方法设置QTime
内容。
用toString()
方法将日期对象转换成字符串输出。
输出结果为:
$ ./init
The date is Sun Apr 12 2015
The date is Tue Mar 3 2015
The time is 17:30:12.055
The time is 13:52:45.155
当然日期和时间
在这个示例中,输出本地的当前时间
// curdatetime.cpp
#include <QTextStream>
#include <QTime>
#include <QDate>
int main(void) {
QTextStream out(stdout);
QDate cd = QDate::currentDate(); // 获取当前日期
QTime ct = QTime::currentTime(); // 获取当前时间
out << "Current date is: " << cd.toString() << endl;
out << "Current time is: " << ct.toString() << endl;
}
注:不能将文件命名为time.cpp
输出结果为:
$ ./curdatetime
Current date is: 周三 2月 14 2018
Current time is: 15:33:16
日期比较
关系运算符可用于比较日期。可以比较他们在日历中的位置
// comparedates.cpp
#include <QTextStream>
#include <QDate>
int main(void) {
QTextStream out(stdout);
QDate dt1(2015, 4, 5);
QDate dt2(2014, 4, 5);
if (dt1 < dt2) {
out << dt1.toString() << " comes before "
<< dt2.toString() << endl;
} else {
out << dt1.toString() << " comes after "
<< dt2.toString() << endl;
}
}
if (dt1 < dt2) {
out << dt1.toString() << " comes before "
<< dt2.toString() << endl;
} else {
out << dt1.toString() << " comes after "
<< dt2.toString() << endl;
}
将日期用小于(<)比较运算符进行比较,并确定它们中的哪些位于日历中较早的位置。
输出结果为:
$ ./comparedates
周日 4月 5 2015 comes after 周六 4月 5 2014
比较运算符也能在QTime
和QDateTime
中使用。
闰年的判定
QDate::isLeapYear()
这个静态方法可以用来判断是否是闰年。
// leapyear.cpp
#include <QTextStream>
#include <QDate>
int main() {
QTextStream out(stdout);
QList<int> years({2010, 2011, 2012, 2013, 2014, 2015, 2016});
foreach (int year, years) {
if (QDate::isLeapYear(year)) {
out << year << " is a leap year" << endl;
} else {
out << year << " is not a leap year" << endl;
}
}
}
QList<int> years({2010, 2011, 2012, 2013, 2014, 2015, 2016});
使用列表初始化的方法,获取一系列年份,这是C++ 11的新特性,需要在.pro
文件内加入CONFIG += c++11
、QMAKE_CXXFLAGS += -std=c++11
或者QMAKE_CXXFLAGS += -std=c++0x
.
foreach (int year, years) {
if (QDate::isLeapYear(year)) {
out << year << " is a leap year" << endl;
} else {
out << year << " is not a leap year" << endl;
}
}
通过对列表进行遍历,判断其中的年份是不是闰年。QDate::isLeapYear()
返回一个布尔类型。
// leapyear.pro
######################################################################
# Automatically generated by qmake (3.1) Wed Feb 14 15:56:59 2018
######################################################################
TEMPLATE = app
TARGET = leapyear
INCLUDEPATH += .
CONFIG += c++11
# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Input
SOURCES += leapyear.cpp
QT -= gui
这个是项目管理文件,用CONFIG += c++11
来添加C++ 11特性,用QT -= gui
禁用GUI模块。
输出结果为:
$ ./leapyear
2010 is not a leap year
2011 is not a leap year
2012 is a leap year
2013 is not a leap year
2014 is not a leap year
2015 is not a leap year
2016 is a leap year