1、获取系统时间,并写入文档中
//注意,读取的时候也要使用相同的类型读出
QFile file(strPresetFilePath);
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QDataStream out(&file); //将数据序列化
out << QDate::currentDate();
file.close();
2、读取时间
//从文件种读取数据
QFile file(strPresetFilePath);
file.setFileName(strPresetFilePath);
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "File open error2";
exit(1);
}
QDataStream in(&file); //从文件中读出数据
QDate birthday;
in >> birthday ; //获取字符串和整数
file.close();
QString str1 = birthday.toString("yyyy-MM-dd");
qDebug() << "str1 = " << str1;
QByteArray ba = str1.toLatin1();//将QString 转换为 char *类型
char *dateStr = ba.data();//将QString 转换为 char *类型
char *year = strtok(dateStr,"-");
char *month = strtok(NULL,"-");
char *date = strtok(NULL,"-");
qDebug() << "year is:" << year;
qDebug() << "month is:" << month;
qDebug() << "date is:" << date;