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

本文介绍了Qt5中处理日期和时间的类,包括初始化日期和时间、日期比较、闰年判断。示例展示了如何创建日期对象、输出当前时间、进行日期比较,并提供了检查闰年的方法。内容涵盖QDate、QTime的使用以及C++11的列表初始化特性。
摘要由CSDN通过智能技术生成

参考:http://zetcode.com/gui/qt5/datetime/
Qt5用于QDate, QTimeQDateTime这三个类处理日期和时间。QDate是处理公历中日期的方法,它有确定日期,比较或操纵日期的方法。QTime是处理时间的方法,它提供了比较时间,确定时间和各种其他操纵时间的方法。QDateTime是一个将QDateQTime对象组合到一个对象中的类。

初始化时间和日期

日期和时间对象可以用两种基本方式初始化。在对象的构造函数中初始化它们,或者可以创建空对象并在以后用数据填充它们。

// 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: 周三 214 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 
周日 45 2015 comes after 周六 45 2014

比较运算符也能在QTimeQDateTime中使用。

闰年的判定

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++11QMAKE_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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值