13,069
回編集
| 1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
Qtにおいて、<code>QDate</code>クラスまたは<code>QTime</code>クラスを使用して、日付および時刻の有効性を確認する。<br> | Qtにおいて、<code>QDate</code>クラスまたは<code>QTime</code>クラスを使用して、日付および時刻の有効性を確認する。<br> | ||
<br><br> | |||
== 日本時間の取得 == | |||
PCのシステム環境のロケールやタイムゾーン設定に基づいて日付を取得しているため、システムが日本時間に設定されている場合は、<br> | |||
<code>QDate::currentDate</code>メソッドにより取得される日付は現地の日付となる。<br> | |||
<br> | |||
タイムゾーンを考慮した日時の操作が必要な場合は、<code>QDateTime</code>クラスおよび<code>QTimeZone</code>クラスを使用することにより、より詳細な制御が可能となる。<br> | |||
<br> | |||
また、日付のみが必要な場合でも、タイムゾーンを考慮するには<code>QDateTime</code>クラスが適している。<br> | |||
<syntaxhighlight lang="c++"> | |||
#include <QDateTime> | |||
#include <QTimeZone> | |||
// 日本のタイムゾーンを設定 | |||
QDateTime now = QDateTime::currentDateTime(); | |||
now.setTimeZone(QTimeZone("Asia/Tokyo")); | |||
// 日本時間での日付と時刻を表示 | |||
std::cout << QString("日本時間での現在の日付と時刻 : %1").arg(now.toString()) << std::endl; | |||
// 現在の年、月、日を取得 | |||
QDate currentDate = now.date(); | |||
int year = currentDate.year(); | |||
int month = currentDate.month(); | |||
int day = currentDate.day(); | |||
std::cout << QString("年 : %1, 月 : %2, 日 : %3").arg(year, month, day).toStdString() << std::endl; | |||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||