您的位置:首页 > 编程语言 > Qt开发

Qt学习(9)——Qt5中的日期(Date)和时间(Time)(3)

2018-02-20 10:54 459 查看
检索星期

计算天数

检查日期的有效性

计算指定日期N天后的日期

检索星期

dayOfWeek()
方法返回一个数字,表示一周的某一天,其中1是星期一,7是星期日。

// weekday.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

QTextStream out(stdout);

QDate cd = QDate::currentDate(); // 获取当前日期
int wd = cd.dayOfWeek(); // 得到今天是星期几

out << "Today is " << QDate::shortDayName(wd) << endl;
out << "Today is " << QDate::longDayName(wd) << endl;
}


使用
QDate::shortDayName()
静态方法,得到了周二的短名称。

使用
QDate::longDayName()
静态方法,得到了周二的全称。

输出结果:

$ ./weekday
Today is 周二
Today is 星期二




在执行
make
之后,会有如下提示:

g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I../../../../Qt/5.10.0/gcc_64/include -I../../../../Qt/5.10.0/gcc_64/include/QtGui -I../../../../Qt/5.10.0/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I../../../../Qt/5.10.0/gcc_64/mkspecs/linux-g++ -o weekday.o weekday.cpp
weekday.cpp: In function ‘int main()’:
weekday.cpp:11:33: warning: ‘static QString QDate::shortDayName(int, QDate::MonthNameType)’ is deprecated: Use QLocale::dayName or QLocale::standaloneDayName [-Wdeprecated-declarations]
out << "Today is " << QDate::shortDayName(wd) << endl;
^~~~~~~~~~~~
In file included from ../../../../Qt/5.10.0/gcc_64/include/QtCore/QDate:1:0,
from weekday.cpp:2:
../../../../Qt/5.10.0/gcc_64/include/QtCore/qdatetime.h:88:24: note: declared here
static QString shortDayName(int weekday, MonthNameType type = DateFormat);
^~~~~~~~~~~~
weekday.cpp:11:48: warning: ‘static QString QDate::shortDayName(int, QDate::MonthNameType)’ is deprecated: Use QLocale::dayName or QLocale::standaloneDayName [-Wdeprecated-declarations]
out << "Today is " << QDate::shortDayName(wd) << endl;
^
In file included from ../../../../Qt/5.10.0/gcc_64/include/QtCore/QDate:1:0,
from weekday.cpp:2:
../../../../Qt/5.10.0/gcc_64/include/QtCore/qdatetime.h:88:24: note: declared here
static QString shortDayName(int weekday, MonthNameType type = DateFormat);
^~~~~~~~~~~~
weekday.cpp:12:33: warning: ‘static QString QDate::longDayName(int, QDate::MonthNameType)’ is deprecated: Use QLocale::dayName or QLocale::standaloneDayName [-Wdeprecated-declarations]
out << "Today is " << QDate::longDayName(wd) << endl;
^~~~~~~~~~~
In file included from ../../../../Qt/5.10.0/gcc_64/include/QtCore/QDate:1:0,
from weekday.cpp:2:
../../../../Qt/5.10.0/gcc_64/include/QtCore/qdatetime.h:92:24: note: declared here
static QString longDayName(int weekday, MonthNameType type = DateFormat);
^~~~~~~~~~~
weekday.cpp:12:47: warning: ‘static QString QDate::longDayName(int, QDate::MonthNameType)’ is deprecated: Use QLocale::dayName or QLocale::standaloneDayName [-Wdeprecated-declarations]
out << "Today is " << QDate::longDayName(wd) << endl;
^
In file included from ../../../../Qt/5.10.0/gcc_64/include/QtCore/QDate:1:0,
from weekday.cpp:2:
../../../../Qt/5.10.0/gcc_64/include/QtCore/qdatetime.h:92:24: note: declared here
static QString longDayName(int weekday, MonthNameType type = DateFormat);
^~~~~~~~~~~
g++ -Wl,-O1 -Wl,-rpath,/home/zhangsl/Qt/5.10.0/gcc_64/lib -o weekday weekday.o   -L/home/zhangsl/Qt/5.10.0/gcc_64/lib -lQt5Gui -lQt5Core -lGL -lpthread


可以看出来,
QDate::shortDayName()
QDate::longDayName()
已经被舍弃了,可以应用
QLocale::dayName
QLocale::standaloneDayName
方法。

不过应用这两个方法,需要
#include <QLocale>
,并且在
.pro
文件内加上
QT += core


// weekdatnew.cpp
#include <QTextStream>
#include <QDate>
#include <QLocale>
int main(void) {

QTextStream out(stdout);

out << "Today is " << QLocale().dayName(QDate::currentDate().dayOfWeek(), QLocale::LongFormat) << endl;
out << "Today is " << QLocale().dayName(QDate::currentDate().dayOfWeek(), QLocale::ShortFormat) << endl;
out << "Today is " << QLocale().standaloneDayName(QDate::currentDate().dayOfWeek(), QLocale::LongFormat) << endl;
out << "Today is " << QLocale().standaloneDayName(QDate::currentDate().dayOfWeek(), QLocale::ShortFormat) << endl;
}


输出结果为:

$ ./weekdaynew
Today is 星期二
Today is 周二
Today is 星期二
Today is 周二


计算天数

可以使用
daysInMonth()
方法计算特定月份中的天数,并使用
daysInYear()
方法计算一年中的天数。

// nofdays.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

QTextStream out(stdout);
QList<QString> months;

months.append("January");
months.append("February");
months.append("March");
months.append("April");
months.append("May");
months.append("June");
months.append("July");
months.append("August");
months.append("September");
months.append("October");
months.append("November");
months.append("December");

QDate dt1(2015, 9, 18);
QDate dt2(2015, 2, 11);
QDate dt3(2015, 5, 1);
QDate dt4(2015, 12, 11);
QDate dt5(2015, 1, 21);

// 获取月份中的天数
out << "There are " << dt1.daysInMonth() << " days in "
<< months.at(dt1.month()-1) << endl;
out << "There are " << dt2.daysInMonth() << " days in "
<< months.at(dt2.month()-1) << endl;
out << "There are " << dt3.daysInMonth() << " days in "
<< months.at(dt3.month()-1) << endl;
out << "There are " << dt4.daysInMonth() << " days in "
<< months.at(dt4.month()-1) << endl;
out << "There are " << dt5.daysInMonth() << " days in "
<< months.at(dt5.month()-1) << endl;

// 获取一年中的天数
out << "There are " << dt1.daysInYear() << " days in year "
<< QString::number(dt1.year()) << endl;
}


使用
daysInMonth()
获取指定月份的天数,使用
daysInYear()
获取指定年份的天数。

输出结果为:

$ ./nofdays
There are 30 days in September
There are 28 days in February
There are 31 days in May
There are 31 days in December
There are 31 days in January
There are 365 days in year 2015


检查日期的有效性

使用
isValid()
方法检查日期是否有效。

// validity.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

QTextStream out(stdout);

// 创建三个日期,前两个有效,后一个无效
QList<QDate> dates({QDate(2015, 5, 11), QDate(2015, 8, 1),
QDate(2015, 2, 30)});

for (int i=0; i < dates.size(); i++) {

if (dates.at(i).isValid()) {
out << "Date " << i+1 << " is a valid date" << endl;
} else {
out << "Date " << i+1 << " is not a valid date" << endl;
}
}
}


输出结果为:

$ ./validity
Date 1 is a valid date
Date 2 is a valid date
Date 3 is not a valid date


计算指定日期N天后的日期

我们可以轻松计算出特定日期n天后的日期。使用
addDays()
方法。
daysTo()
方法返回选定日期的天数。

// daystofrom.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

QTextStream out(stdout);

QDate dt(2018, 2, 20);
// addDays()方法返回给定日期后55天的QDate日期。
QDate nd = dt.addDays(55);

QDate xmas(2018, 12, 24);

out << "55 days from " << dt.toString() << " is "
<< nd.toString() << endl;

// 我们使用daysTo()方法计算圣诞节前的天数。
out << "There are " << QDate::currentDate().daysTo(xmas)
<< " days till Christmas" << endl;
}


输出结果为:

$ ./daystofrom
55 days from 周二 2月 20 2018 is 周一 4月 16 2018
There are 307 days till Christmas
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Qt
相关文章推荐