本页内容

QCalendar Class

QCalendar 类用于描述日历系统。更多内容...

头文件: #include <QCalendar>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数均为可重入函数。

公共类型

(since 6.2) class SystemId
enum class System { Gregorian, Julian, Milankovic, Jalali, IslamicCivil }

公共函数

QCalendar()
QCalendar(QAnyStringView name)
QCalendar(QCalendar::System system)
(since 6.2) QCalendar(QCalendar::SystemId id)
QDate dateFromParts(const QCalendar::YearMonthDay &parts) const
QDate dateFromParts(int year, int month, int day) const
QString dateTimeToString(QStringView format, const QDateTime &datetime, QDate dateOnly, QTime timeOnly, const QLocale &locale) const
int dayOfWeek(QDate date) const
int daysInMonth(int month, int year = Unspecified) const
int daysInYear(int year) const
bool hasYearZero() const
bool isDateValid(int year, int month, int day) const
bool isGregorian() const
bool isLeapYear(int year) const
bool isLunar() const
bool isLuniSolar() const
bool isProleptic() const
bool isSolar() const
bool isValid() const
(since 6.7) QDate matchCenturyToWeekday(const QCalendar::YearMonthDay &parts, int dow) const
int maximumDaysInMonth() const
int maximumMonthsInYear() const
int minimumDaysInMonth() const
QString monthName(const QLocale &locale, int month, int year = Unspecified, QLocale::FormatType format = QLocale::LongFormat) const
int monthsInYear(int year) const
QString name() const
QCalendar::YearMonthDay partsFromDate(QDate date) const
QString standaloneMonthName(const QLocale &locale, int month, int year = Unspecified, QLocale::FormatType format = QLocale::LongFormat) const
QString standaloneWeekDayName(const QLocale &locale, int day, QLocale::FormatType format = QLocale::LongFormat) const
QString weekDayName(const QLocale &locale, int day, QLocale::FormatType format = QLocale::LongFormat) const

静态公共成员

QStringList availableCalendars()

详细说明

QCalendar 对象根据特定系统的规则,将年、月和日号映射到特定的一天(最终由其儒略日号来确定)。

默认的 QCalendar() 是一个前推式公历,其中没有零年。通过启用相应功能或加载插件,可以支持其他日历。作为功能支持的日历可以通过向构造函数传递QCalendar::System 枚举来构建。 所有受支持的日历在初始化后均可通过名称进行创建。(因此,插件会实例化其日历后端以进行注册。)可通过 `QCalendar::System` 访问的内置后端,也始终可通过名称获取。使用自定义后端的日历也可使用在创建后端时分配的唯一 ID 进行创建。

QCalendar 值是不可变的。

另请参阅 QDate 和QDateTime 。

成员类型文档

enum class QCalendar::System

此枚举类型用于指定日历系统的选项。

常量值描述
QCalendar::System::Gregorian0默认日历,国际通用。
QCalendar::System::Julian8古罗马历法。
QCalendar::System::Milankovic9一些东正教教会使用的修订版儒略历。
QCalendar::System::Jalali10太阳希吉拉历(也称波斯历)。
QCalendar::System::IslamicCivil11(表格形式的)伊斯兰民历。

另请参阅 QCalendar 和QCalendar::SystemId 。

成员函数文档

[explicit] QCalendar::QCalendar()

[explicit] QCalendar::QCalendar(QAnyStringView name)

[explicit] QCalendar::QCalendar(QCalendar::System system)

创建一个日历对象。

日历的选择可通过 `system` 并使用枚举 `QCalendar::System` 指定,或通过 `name` 并使用字符串(Unicode 或 Latin 1)指定。按名称构造可能取决于是否已通过其他方式先构建了所给日历的实例。若不带参数,默认构造函数将返回公历。

注意:在 Qt 6.4 之前的版本中,通过name 调用的构造函数仅接受QStringView 和QLatin1String ,不接受QAnyStringView 。

另请参见 QCalendar 、System 和isValid()。

[explicit, since 6.2] QCalendar::QCalendar(QCalendar::SystemId id)

创建一个日历对象。

当使用自定义日历实现时,其后端在创建时会被分配一个唯一的ID;将该ID作为id 传递给此构造函数,即可获得一个使用该后端的QCalendar对象。当后端未按名称注册时,此方法非常有用。

这是一个重载函数。

该函数在 Qt 6.2 中引入。

[static] QStringList QCalendar::availableCalendars()

返回一个包含可用日历系统名称的列表。

这些日历系统可能由插件或链接到应用程序中的其他代码提供,此外还有 Qt 提供的日历系统,其中部分由功能模块控制。

QDate QCalendar::dateFromParts(const QCalendar::YearMonthDay &parts) const

QDate QCalendar::dateFromParts(int year, int month, int day) const

将年、月和日转换为QDate 。

year 、month 和day 可以作为单独的数字传递,也可以打包为parts 的成员。如果该日历中存在对应的年、月和日期,则返回一个包含这些信息的QDate 。 否则(包括任何参数值为 QCalendar::Unspecified 的情况),将返回一个 isNull() 为 true 的QDate 。如果所表示的日期超出QDate 支持的范围,则返回的QDate 的isValid() 应为 false。

另请参阅 isDateValid() 和partsFromDate()。

QString QCalendar::dateTimeToString(QStringView format, const QDateTime &datetime, QDate dateOnly, QTime timeOnly, const QLocale &locale) const

返回一个字符串,该字符串表示给定的日期、时间或日期时间。

如果datetime 有效,则将其表示出来,并识别日期和时间字段的格式指定符;否则,如果dateOnly 有效,则将其表示出来,并仅识别日期字段的格式指定符;最后,如果timeOnly 有效,则将其表示出来,并仅识别时间字段的格式指定符。 如果上述任何一种格式均无效,则返回空字符串。

有关受支持的字段指定符,请参阅QDate::toString 和QTime::toString()。format 中被识别为字段指定符的字符将被替换为代表所表示的日期和/或时间的相应数据的文本。代表它们的文本可能取决于指定的locale 。format 中的其他字符将原样复制到返回的字符串中。

另请参阅 monthName()、weekDayName()、QDate::toString() 以及QTime::toString()。

int QCalendar::dayOfWeek(QDate date) const

返回给定日期date 对应的星期几编号。

如果日历无法表示指定的日期,则返回零。周一至周日分别返回1至7。包含闰日的日历可能使用其他数字来表示这些日期。

另请参阅 partsFromDate() 和Qt::DayOfWeek 。

int QCalendar::daysInMonth(int month, int year = Unspecified) const

返回给定year 中month 的日数。

月份按顺序编号,每年的第一个月编号为1。如果year 是Unspecified (若未传入参数,则默认使用该值),则返回该月份在任何一年中的最长天数。

另请参阅 maximumDaysInMonth() 和minimumDaysInMonth()。

int QCalendar::daysInYear(int year) const

返回给定的year 中的天数。

将Unspecified 作为year 进行处理的行为未定义。

bool QCalendar::hasYearZero() const

如果该日历包含零年,则返回true 。

一种历法可能从其第一年开始表示年份,但不提供描述第一年之前年份的方法;这种历法没有零年,也不属于推算历。

若某历法表示其起始年之前的年份,则可直接遵循常规整数计数方式对这些年份进行编号,即起始年前的一年为零年,其前为负数年份;此类历法属于推算历,且具有零年。 一种历法也可能设有零年(例如,以某重大事件发生之年为零年,此后各年依次为该事件后的第一年、第二年等),却不标注零年之前的年份。此类历法虽有零年,但并非推算历。

然而,有些历法会采用替代编号来表示其起始年之前的年份;例如,推算式公历的元年是公元1年,其前一年是公元前1年,再之前是公元前2年,以此类推。 在这种情况下,我们使用负数年号进行这种替代编号,其中-1年是1年前的一年,-2年是-1年前的一年,以此类推。这种历法属于推算历,但没有零年。

另请参见 isProleptic()。

bool QCalendar::isDateValid(int year, int month, int day) const

当给定的year 、month 和day 指定了该日历中的有效日期时,该函数精确返回true 。

通常这意味着 1 <= month <=monthsInYear(年) 且 1 <= day <=daysInMonth(月, 年)。然而,包含闰日或闰月的日历可能会使情况变得复杂。

bool QCalendar::isGregorian() const

如果该日历对象是其他 Qt API(例如在QDate 中)用作默认日历的公历对象,则返回 `true `。

bool QCalendar::isLeapYear(int year) const

如果给定的year 是闰年,则返回true 。

由于一年并非由整数天数组成,因此有些年份比其他年份更长。这种差异可能是整整一个月,也可能是仅仅一天;具体情况因历法而异。

另请参阅 isDateValid()。

bool QCalendar::isLunar() const

如果该日历是农历,则返回true 。

阴历是指主要基于月相的历法。

bool QCalendar::isLuniSolar() const

如果该日历是阴阳历,则返回true 。

阴阳历既能反映月相变化,又能适应太阳相对于恒星在天球上位置的变化。

bool QCalendar::isProleptic() const

如果该日历是前推日历,则返回true 。

前推历能够描述其起始年之前任意远期的年份。这些年份由负数年份表示,有时也会使用零年。

另请参阅 hasYearZero()。

bool QCalendar::isSolar() const

如果该日历为太阳历,则返回true 。

太阳历主要基于太阳在天球上相对于恒星的位置变化。

bool QCalendar::isValid() const

如果这是一个有效的日历对象,则返回 true。

使用无法识别的日历名称创建日历可能会导致生成无效对象。在按名称创建日历后,请使用此方法进行检查。

[since 6.7] QDate QCalendar::matchCenturyToWeekday(const QCalendar::YearMonthDay &parts, int dow) const

根据给定的星期几调整日期的世纪。

当给定日期的星期、日期、月份和年份的后两位数字时使用。 返回一个QDate 实例,其dayOfWeek()为给定的dow ,且month和day of the month与给定的parts 相匹配。返回的QDate 的year()应与parts.year 相差100的倍数,且优先选择较小的倍数而非较大的倍数,并优先选择正倍数而非其负数。

如果没有日期符合这些条件,则返回一个无效的QDate :该星期几与给定的其他数据不兼容。 例如,这种情况在公历中会出现,因为公历的400年周期恰好是整数周数,因此对于给定最后两位数字的年份,任何特定的月份及其具体日期,在该年份中只会落在四个特定的星期上。 (在世纪之交的特殊情况下,若2月29日所在的年份为闰年,则该日仅可能对应一种星期:星期二。)

该函数在 Qt 6.7 中引入。

int QCalendar::maximumDaysInMonth() const

返回日历中任意一年里天数最多的那个月的天数。

另请参阅 daysInMonth() 和minimumDaysInMonth()。

int QCalendar::maximumMonthsInYear() const

返回任何一年可能包含的最大月数。

另请参阅 monthName()、standaloneMonthName() 和monthsInYear()。

int QCalendar::minimumDaysInMonth() const

返回日历中任意一年里天数最少的月份的天数。

另请参阅 daysInMonth() 和maximumDaysInMonth()。

QString QCalendar::monthName(const QLocale &locale, int month, int year = Unspecified, QLocale::FormatType format = QLocale::LongFormat) const

返回一个经过适当本地化的月份名称。

月份由数字表示,其中month = 1 表示当年的第一个月,后续月份依次编号。如果month 的数字无法识别,则返回空字符串。

year 可以是“未指定”,在这种情况下,应使用典型年份中月份数字与名称的映射关系。某些日历中的闰月并不总是在年末;此时,月份数字与名称的映射可能取决于闰月的位置。因此,如果已知年份,通常应予以指定。

locale名称以该日历中通常用于完整日期的形式返回;format 决定其表达的完整程度(即缩写程度)。

另请参阅 standaloneMonthName()、maximumMonthsInYear() 以及dateTimeToString()。

int QCalendar::monthsInYear(int year) const

返回给定year 中的月份数。

如果 `year ` 为 `Unspecified`,则返回一年中的最大月数。

另请参阅 maximumMonthsInYear()。

QString QCalendar::name() const

此日历的主要名称。

该日历可能还有一些别名。通过名称实例化的日历可以使用此类别名,在这种情况下,其 name() 方法的返回值不必与实例化时使用的别名完全一致。

QCalendar::YearMonthDay QCalendar::partsFromDate(QDate date) const

将QDate 转换为年、月和日期。

如果日历无法表示给定的date (或其年份超出int 可表示的范围),则返回结构中的isValid()应为false。否则,其year、month和day成员将记录该表示形式的相应部分。

另请参阅 dateFromParts()、isProleptic() 和hasYearZero()。

QString QCalendar::standaloneMonthName(const QLocale &locale, int month, int year = Unspecified, QLocale::FormatType format = QLocale::LongFormat) const

返回一个经过适当本地化处理的、独立的月份名称。

月份由数字表示,其中month = 1 表示当年的第一个月,后续月份依次编号。如果month 的数字无法识别,则返回空字符串。

year 可以为“Unspecified”(未指定),在这种情况下,应使用典型年份中月份数字到名称的映射关系。某些日历包含闰月,且闰月并不总位于年末;此时,月份数字到名称的映射可能取决于闰月的位置。因此,如果已知年份,通常应指定该年份。

名称以在指定日历体系(locale )中独立使用时的形式返回;日历名称格式(format )决定了其表达的完整程度(即缩写程度)。

另请参阅 monthName()、maximumMonthsInYear() 以及dateTimeToString()。

QString QCalendar::standaloneWeekDayName(const QLocale &locale, int day, QLocale::FormatType format = QLocale::LongFormat) const

返回一个经过适当本地化的、表示某一周几的独立名称。

星期几的编号从 1(星期一)到 7(星期日)。某些日历可能为其他日子(例如不属于任何一周的闰日)支持更高的编号。如果day 编号无法识别,则返回空字符串。

返回的名称采用在指定locale 中独立使用时的格式(例如,作为日历中按月显示的表格中的列标题,其中连续的周作为行);format 决定名称应以何种程度完整表达(即缩写程度)。

另请参阅 weekDayName()和dayOfWeek()。

QString QCalendar::weekDayName(const QLocale &locale, int day, QLocale::FormatType format = QLocale::LongFormat) const

返回一周中某一天的本地化名称。

星期几的编号从 1(星期一)开始,到 7(星期日)结束。某些日历可能为其他日子(例如不属于任何一周的闰日)支持更高的编号。如果未识别该day 编号,则返回空字符串。

返回的名称采用指定locale 中通常用于完整日期的格式;format 决定了名称的完整程度(即缩写程度)。

另请参阅 standaloneWeekDayName() 和dayOfWeek()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.