Qt學習(9)——Qt5中的日期(Date)和時間(Time)(3)

檢索星期

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 functionint 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::dayNameQLocale::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 週二 220 2018 is 週一 416 2018
There are 307 days till Christmas
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章