Qt时间类、日期类、时间日期类介绍

一.时间类(QTime)

Qt中的时间类QTime是用来处理时间的类,它可以表示一个特定的时间,精确到毫秒。QTime类提供了一些方法来访问和操作时间,例如获取小时、分钟、秒以及毫秒部分,还可以进行时间的比较和运算。

下面是一个简单的例子,展示如何使用QTime类创建一个时间对象,并获取时间的各个部分:

#include <QTime>
#include <QDebug>

int main() {
    // 创建一个QTime对象表示当前时间
    QTime currentTime = QTime::currentTime();

    // 输出当前时间的小时、分钟、秒和毫秒部分
    qDebug() << "Current time: " << currentTime.toString();
    qDebug() << "Hour: " << currentTime.hour();
    qDebug() << "Minute: " << currentTime.minute();
    qDebug() << "Second: " << currentTime.second();
    qDebug() << "Millisecond: " << currentTime.msec();

    return 0;
}

 QTime类在Qt中提供了丰富的方法来进行时间的比较、运算以及格式化输出。下面分别详细说明这些方法,并举例演示它们的用法:

 时间比较:

  • QTime::operator==():比较两个时间是否相等。
  • QTime::operator!=():比较两个时间是否不相等。
  • QTime::operator<()QTime::operator>()QTime::operator<=()QTime::operator>=():比较两个时间的大小关系。
#include <QTime>
#include <QDebug>

int main() {
    QTime time1(10, 30, 0);
    QTime time2(12, 0, 0);

    if (time1 < time2) {
        qDebug() << "time1 is earlier than time2";
    } else {
        qDebug() << "time2 is earlier than time1";
    }

    return 0;
}

 时间运算:

  • QTime::addSecs():增加秒数。
  • QTime::addMSecs():增加毫秒数。
  • QTime::secsTo():计算两个时间之间的秒数差。
  • QTime::msecsTo():计算两个时间之间的毫秒数差。
#include <QTime>
#include <QDebug>

int main() {
    QTime time1(10, 30, 0);
    QTime time2 = time1.addSecs(3600); // 增加1小时

    qDebug() << "New time: " << time2.toString();

    int secsDiff = time1.secsTo(time2);
    qDebug() << "Seconds difference: " << secsDiff;

    return 0;
}

二.QTime和QString的转换

在Qt中,可以通过QTime类的toString()方法将QTime对象转换为QString对象,也可以通过QTime类的静态方法fromString()QString对象转换为QTime对象。

QTime转QString:

#include <QTime>
#include <QString>
#include <QDebug>

int main() {
    // 创建一个QTime对象表示特定时间
    QTime time(12, 30, 0);

    // 将QTime对象转换为QString对象
    QString timeString = time.toString("hh:mm:ss");

    // 输出转换后的字符串
    qDebug() << "Time as QString: " << timeString;

    return 0;
}

在上面的例子中,通过调用toString()方法将QTime对象time转换为QString对象timeString,并指定了格式化字符串"hh:mm:ss"来表示小时、分钟和秒的格式。

QString转QTime: 

#include <QTime>
#include <QString>
#include <QDebug>

int main() {
    // 创建一个QString对象表示时间字符串
    QString timeString = "15:45:30";

    // 将QString对象转换为QTime对象
    QTime time = QTime::fromString(timeString, "hh:mm:ss");

    // 输出转换后的QTime对象
    qDebug() << "Time from QString: " << time.toString();

    return 0;
}

在上面的例子中,通过调用fromString()静态方法将QString对象timeString转换为QTime对象time,并指定了格式化字符串"hh:mm:ss"来解析小时、分钟和秒的部分。

通过这种方式,可以方便地在QTime对象和QString对象之间进行转换,便于在Qt应用程序中处理时间数据。

三.日期类(QDate) 

在Qt中,日期类QDate用于处理日期数据,提供了许多方法来操作、比较和格式化日期。下面是对QDate类的详细分析以及一些示例

 QDate类的常用方法和功能:

  1. 构造函数

    • QDate(int year, int month, int day):根据年、月、日创建一个QDate对象。
  2. 获取日期信息

    • year():获取年份。
    • month():获取月份。
    • day():获取日期。
  3. 比较日期

    • operator==()operator!=()operator<()operator>()operator<=()operator>=():用于比较两个日期的大小关系。
  4. 日期运算

    • addDays(int days):增加指定天数。
    • addMonths(int months):增加指定月数。
    • addYears(int years):增加指定年数。
    • daysTo(const QDate &date):计算两个日期之间的天数差。
  5. 格式化输出

    • toString(const QString &format):将日期转换为字符串,可以指定格式化字符串。
  6. 其他方法

    • isValid():检查日期是否有效。
    • currentDate():获取当前日期。
    • fromString(const QString &string, const QString &format):从字符串中解析日期。

示例代码如下 :

#include <QDate>
#include <QDebug>

int main() {
    // 创建一个QDate对象表示特定日期
    QDate date(2024, 5, 19);

    // 输出日期信息
    qDebug() << "Year: " << date.year();
    qDebug() << "Month: " << date.month();
    qDebug() << "Day: " << date.day();

    // 比较日期
    QDate currentDate = QDate::currentDate();
    if (date < currentDate) {
        qDebug() << "The specified date is in the past.";
    } else {
        qDebug() << "The specified date is in the future.";
    }

    // 日期运算
    QDate newDate = date.addDays(10);
    qDebug() << "New date: " << newDate.toString("yyyy-MM-dd");

    // 格式化输出
    qDebug() << "Date as string: " << date.toString("dd/MM/yyyy");

    return 0;
}

四:QDate和QString的转换

在Qt中,可以使用两种方式进行QDateQString之间的转换:一种是使用QDateQString类提供的方法,另一种是使用QVariant类。

1. 使用QDate和QString类的方法进行转换:

QDate转换为QString:

  • 使用QDate::toString(const QString &format)方法将QDate对象转换为字符串。

QString转换为QDate:

  • 使用QDate::fromString(const QString &string, const QString &format)方法将字符串转换为QDate对象。

示例代码如下: 

#include <QDate>
#include <QDebug>

int main() {
    // QDate转换为QString
    QDate date(2023, 10, 15);
    QString dateString = date.toString("yyyy-MM-dd");
    qDebug() << "Date as string: " << dateString;

    // QString转换为QDate
    QString str = "2022-08-25";
    QDate newDate = QDate::fromString(str, "yyyy-MM-dd");
    qDebug() << "String to date: " << newDate.toString("yyyy-MM-dd");

    return 0;
}

2. 使用QVariant进行转换:

QDate转换为QString:

  • 使用QVariant::fromValue()QDate对象转换为QVariant对象,再使用QVariant::toString()QVariant对象转换为字符串。

QString转换为QDate:

  • 使用QVariant::fromValue()将字符串转换为QVariant对象,再使用QVariant::value<QDate>()QVariant对象转换为QDate对象。

示例代码如下: 

#include <QDate>
#include <QVariant>
#include <QDebug>

int main() {
    // QDate转换为QString
    QDate date(2023, 10, 15);
    QVariant variant = QVariant::fromValue(date);
    QString dateString = variant.toString();
    qDebug() << "Date as string: " << dateString;

    // QString转换为QDate
    QString str = "2022-08-25";
    QVariant newVariant = QVariant::fromValue(str);
    QDate newDate = newVariant.value<QDate>();
    qDebug() << "String to date: " << newDate.toString("yyyy-MM-dd");

    return 0;
}

 五:时间日期类(QDateTime)

在Qt中,QDateTime类用于表示日期和时间的组合。它包含了日期和时间的信息,并提供了一系列方法来处理日期和时间数据。以下是QDateTime类的一些主要特性和示例:

 主要特性:

  1. 构造函数QDateTime类有多个构造函数,可以根据需要传递日期、时间、时区等参数来创建对象。

  2. 获取日期时间信息

    • date():获取日期部分。
    • time():获取时间部分。
    • timeZone():获取时区信息。
  3. 比较和运算

    • 可以比较两个QDateTime对象的大小。
    • 可以对QDateTime对象进行加减操作,例如addDays()addSecs()addMonths()等。
  4. 格式化输出

    • toString(const QString &format):将QDateTime对象格式化为字符串。
  5. 其他方法

    • isValid():检查日期时间是否有效。
    • currentDateTime():获取当前日期时间。
    • fromString(const QString &string, const QString &format):从字符串中解析日期时间。

代码示例如下: 

#include <QDateTime>
#include <QDebug>

int main() {
    // 创建一个QDateTime对象表示特定日期时间
    QDateTime datetime(QDate(2024, 5, 19), QTime(12, 30, 0));

    // 输出日期时间信息
    qDebug() << "Date: " << datetime.date().toString("yyyy-MM-dd");
    qDebug() << "Time: " << datetime.time().toString("hh:mm:ss");

    // 比较日期时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    if (datetime < currentDateTime) {
        qDebug() << "The specified date time is in the past.";
    } else {
        qDebug() << "The specified date time is in the future.";
    }

    // 日期时间运算
    QDateTime newDateTime = datetime.addDays(5).addSecs(3600); // 增加5天和1小时
    qDebug() << "New date time: " << newDateTime.toString("yyyy-MM-dd hh:mm:ss");

    // 格式化输出
    qDebug() << "Date time as string: " << datetime.toString("yyyy-MM-dd hh:mm:ss");

    return 0;
}

六:QDate和QString的转换

在Qt中,可以使用QDateTime类来表示日期和时间,并且可以与QString进行相互转换。以下是关于日期时间类QDateTimeQString之间转换的详细说明以及示例:

QDateTime转换为QString:

1.使用toString()方法QDateTime类提供了toString()方法,可以将日期时间对象转换为字符串,可以指定格式。

QDateTime dateTime(QDate(2024, 5, 19), QTime(12, 30, 0)); // 创建一个日期时间对象
QString dateTimeString = dateTime.toString("yyyy-MM-dd hh:mm:ss"); // 转换为字符串
qDebug() << "DateTime as string: " << dateTimeString;

 2.使用QVariant:可以通过QVariant::fromValue()QDateTime对象转换为QVariant对象,再使用QVariant::toString()QVariant对象转换为字符串。

QDateTime dateTime(QDate(2024, 5, 19), QTime(9, 0, 0));
QVariant variant = QVariant::fromValue(dateTime);
QString dateTimeString = variant.toString();
qDebug() << "DateTime as string: " << dateTimeString;

QString转换为QDateTime:

1.使用QDateTime::fromString()静态方法:可以通过QDateTime::fromString()静态方法将字符串转换为QDateTime对象。

QString dateTimeStr = "2024-05-19 17:20:30";
QDateTime newDateTime = QDateTime::fromString(dateTimeStr, "yyyy-MM-dd hh:mm:ss");
qDebug() << "String to DateTime: " << newDateTime.toString();

 2.使用QVariant:可以通过QVariant::fromValue()将字符串转换为QVariant对象,再使用QVariant::value<QDateTime>()QVariant对象转换为QDateTime对象。

QString dateTimeStr = "2024-05-19 12:15:00";
QVariant newVariant = QVariant::fromValue(dateTimeStr);
QDateTime newDateTime = newVariant.value<QDateTime>();
qDebug() << "String to DateTime: " << newDateTime.toString();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/634964.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Java ( 框架界面 , 按钮 , 动作监听ActionListener ,鼠标监听MouseListener,键盘监听KeyListener)的使用方法

package 拼图阶段任务.ui;import javax.swing.*; import java.awt.*; import java.awt.event.*;public class UseMethod {public static void main(String[] args) { // 框架的用法JFrame jf new JFrame();// 设置界面的宽高jf.setSize(603,680);// 设置界面的标题jf.setTitle…

Redis的下载、安装、启动和初尝试【超级简单】

redis最好是在Linux系统中使用&#xff0c;这是最接近生产实际的环境。 不过&#xff0c;我们初学者&#xff0c;目的是学习Redis的使用、原理&#xff0c;如果在Linux下直接学习Redis&#xff0c;很可能会因为命令不熟悉而劝退&#xff0c;这是不好的。 因此&#xff0c;我主张…

国际货币基金组织警告:网络攻击影响全球金融稳定

近日&#xff0c;在一份关于金融稳定的报告中&#xff0c;国际货币基金组织&#xff08;IMF&#xff09;用了一章&#xff08;共三章&#xff09;的篇幅描述了网络攻击对金融环境的影响&#xff0c;并警告称&#xff0c;全球金融稳定正受到日益频繁和复杂的网络攻击的威胁。同时…

kubernetes(k8s) v1.30.1 创建本地镜像仓库 使用本地docker镜像仓库部署服务 Discuz X3.5 容器搭建论坛

1 master11创建本地镜像仓库 [rootmaster11 ~]# docker run -d -p 5000:5000 --restartalways --name registry registry:2 Unable to find image registry:2 locally 2: Pulling from library/registry 79e9f2f55bf5: Pull complete 0d96da54f60b: Pull complete 5b27040df…

是德科技 DSOS104A MSOS104A示波器

产品 带宽 通道数 最大存储器深度 DSOS104A 高清晰度示波器 1 GHz 4 个模拟通道 800 Mpts MSOS104A 高清晰度示波器 1 GHz 4 个模拟通道和 16 个数字通道 800 Mpts 商品介绍 …

ELK 日志监控平台(一)- 快速搭建

文章目录 ELK 日志监控平台&#xff08;一&#xff09;- 快速搭建1.ELK 简介2.Elasticsearch安装部署3.Logstash安装部署4.Kibana安装部署5.日志收集DEMO5.1.创建SpringBoot应用依赖导入日志配置文件 logback.xml启动类目录结构启动项目 5.2.创建Logstash配置文件5.3.重新启动L…

锁相环的一些学习笔记--(1)

下图两组1.2.3可以对应起来&#xff1b; 参考资料&#xff1a; 1. Matlab https://www.bilibili.com/video/BV1bR4y1Z7Xg/?spm_id_from333.1296.top_right_bar_window_history.content.click&vd_source5556680656536651c49f5e55d7d4df23 2. https://www.bilibili.com/…

Hadoop开发之JavaAPI操作HDFS

目录 一、maven的安装与配置1.maven的下载2.maven的安装与配置&#xff08;1&#xff09;解压&#xff08;2&#xff09;创建本地仓库文件夹&#xff08;3&#xff09;修改settings.xml配置文件&#xff08;4&#xff09;配置maven的环境变量&#xff08;5&#xff09;idea中ma…

将list对象里的某一个属性取出组成一个新的list

使用Java8将对象里的某一个属性取出组成一个新的list List<Spgg1> listnew ArrayList<>();Spgg1 spgg1new Spgg1();spgg1.setSpdm("测试");spgg1.setGgdm("001");list.add(spgg1);Spgg1 spgg2new Spgg1();spgg2.setSpdm("测试2");sp…

不用从头训练,通过知识融合创建强大的统一模型

在自然语言处理&#xff08;NLP&#xff09;领域&#xff0c;大型语言模型&#xff08;LLMs&#xff09;的开发和训练是一个复杂且成本高昂的过程。数据需求是一个主要问题&#xff0c;因为训练这些模型需要大量的标注数据来保证其准确性和泛化能力&#xff1b;计算资源也是一个…

基于 Prometheus 的超算弹性计算场景下主机监控最佳实践

作者&#xff1a;左知 超算场景的业务特点 主机监控&#xff0c;或许是监控/可观测领域最传统和普遍的需求。在超算训练&#xff0c;AI 大规模训练的业务场景下&#xff0c;主机监控又有哪些痛点和难点呢&#xff1f;根据我们针对多个大规模超算客户的需求整理&#xff0c;超…

LeetCode:柱状图中最大的矩形

文章收录于LeetCode专栏 LeetCode地址 柱状图中最大的矩形 题目 给定 n 个非负整数&#xff0c;用来表示柱状图中各个柱子的高度。每个柱子彼此相邻&#xff0c;且宽度为 1 。求在该柱状图中&#xff0c;能够勾勒出来的矩形的最大面积。 示例1 **输入&#xff1a;**heights …

FastAPI单元测试:使用TestClient轻松测试你的API

当使用FastAPI进行单元测试时&#xff0c;一个重要的工具是TestClient类。TestClient类允许我们模拟对FastAPI应用程序的HTTP请求&#xff0c;并测试应用程序的响应。这使我们能够在不启动服务器的情况下对API进行全面的测试。 下面我将详细讲解TestClient的使用方法和常见操作…

empirecms 文件上传 (CVE-2018-18086)

漏洞环境&#xff1a;vulfocus 到管理后台 使用用户名密码&#xff1a;admin:123456登录&#xff0c;然后到后台找到文件上传点 发现需要后缀名为.mod 创建生成一句话木马的php脚本&#xff0c;并添加.mod后缀名&#xff0c;然后提交 <?php file_put_contents("shell…

攻防世界---web---warmup

1、题目描述 2、查看源码&#xff0c;发现有个source.php 3、访问该文件&#xff0c;得到这一串代码 4、分析代码 5、访问hint.php&#xff0c;提示flag在ffffllllaaaagggg这个文件下 6、构造payload ?filesource.php?/../../../../../../ffffllllaaaagggg

网络性能与流量监控:优化企业网络管理的关键策略

目录 网络性能监控的重要性 1. 提高网络可靠性 2. 优化网络资源使用 3. 提升用户体验 网络流量监控的必要性 1. 识别异常流量 2. 改善网络管理 3. 确保合规性 AnaTraf网络流量分析仪&#xff1a;提升网络监控效率的利器 如何实施有效的网络监控策略 1. 确定监控目标…

php发送短信功能(创蓝短信)

一、以下是创蓝发送短信的功能&#xff0c;可以直接执行&#xff1a; <?php$phone 12312312312;$msg 测试短信功能;echo 发送手机号&#xff1a;.$phone.<br/>;echo 发送内容&#xff1a;.$msg.<br/>;$send sendMessage($phone, $msg);var_dump($send);…

2024电工杯数学建模 - 案例:最短时间生产计划安排

# 前言 2024电工杯(中国电机工程学会杯)数学建模思路解析 最新思路更新(看最新发布的文章即可): https://blog.csdn.net/dc_sinor/article/details/138726153 最短时间生产计划模型 该模型出现在好几个竞赛赛题上&#xff0c;预测2022今年国赛也会与该模型相关。 1 模型描…

BUUCTF---misc---我吃三明治

1、下载附件是一张图片 2、在winhex分析&#xff0c;看到一串整齐的编码有点可疑&#xff0c;保存下来&#xff0c;拿去解码&#xff0c;发现解不了&#xff0c;看来思路不对 3、再仔细往下看的时候也发现了一处这样的编码&#xff0c;但是这次编码后面多了一段base编码 4、拿去…

抖音小店什么产品最好卖?六月份的必爆产品!商家抓紧上架!

哈喽~我是电商月月 做抖音小店&#xff0c;爆款是非常吃香的&#xff0c;但普通玩家只有在爆款出来的那几天才能发现&#xff0c;再去截流&#xff0c;其实热度已经不高了&#xff0c;那想吃到这一口“螃蟹”只能自己去挖掘 每年爆的产品就是那几种&#xff0c;我们可以朝这几…