VC++ MFC编程,如何获取当前系统时间

VC++ MFC编程,如何获取当前系统时间,第1张

1、使用CTime类

#include "afxh"
void main()
{
    CString str; //获取系统时间
    CTime tm;
    tm=CTime::GetCurrentTime();   
    str=tmFormat("现在时间是%Y年%m月%d日%X");
    MessageBox(NULL,str,NULL,MB_OK);
}

解析:
    CTime,CString共同用的头文件为“afxh”,
    CTime类可以提取系统的当前时间函数GetCurrentTime(),并且可以通过Format方法转换成CString,如下例
    CTime tmSCan = CTime::GetCurrentTime();
    CString szTime = tmScanFormat("'%Y-%m-%d %H:%M:%S'");
2、得到系统时间日期(使用GetLocalTime)

#include "afxh"
void main()
{
    SYSTEMTIME st;   
    CString strDate,strTime;   
    GetLocalTime(&st);   
    strDateFormat("M----",stwYear,stwMonth,stwDay);   
    strTimeFormat("-:-:-",stwHour,stwMinute,stwSecond);
    printf("%s\n",strDate);
    printf("%s\n",strTime);
}

解析:
    利用GetLocalTime函数,获取系统当前时间,并将获得的值放在SYSTEMTIME结构中,
    同样的也可以使用Format方法,不过这里使用的是CString类的Format方法
    SYSTEMTIME   st;
    GetLocalTime(&st);
    CString   time;
    timeFormat( "M-d-d     d:d:d ",   stwYear);
3、使用GetTickCount//获取系统运行时间,也可以实现对程序的运行时间的计算

#include "afxh"
#include "afxwinh"
void main()
{
    CString str,str1;
//     long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)   
//     Sleep(500);     
//     long t2=GetTickCount();//程序段结束后取得系统运行时间(ms)    
//     strFormat("time:%dms",t2-t1);//前后之差即 程序运行时间   
//     AfxMessageBox(str);
   
    long t=GetTickCount(); //获取系统当前时间      
    str1Format("系统已运行 %d时",t/3600000);   
    str=str1;    
    t%=3600000;   
    str1Format("%d分",t/60000);   
    str+=str1;   
    t%=60000;   
    str1Format("%d秒",t/1000);
    str+=str1;
    AfxMessageBox(str);
}


解析:GetTickCount函数可以获取系统运行的时间,利用其差值可以获取程序的运行时间

vb和java的时间表示方案不同。
java中的时间用long型整数表示,是从1970-1-1 0:0:0开始的毫秒数;
vb中的时间用double型浮点数表示,整数部分是天数(从什么时候开始不清楚,好像不是1970-1-1了?),小数部分是一天内毫秒数的换算。
所以可以这个样子计算:
millsecs=cdbl(thetime)864001000 ' 86400是一天的秒数,1000是毫秒数

有两种方法:
方法一:用javautilDate类来实现,并结合javatextDateFormat类来实现时间的格式化,看下面代码:
import javautil;
import javatext;
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = CalendargetInstance();

DateFormat d1 = DateFormatgetDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String str1 = d1format(now);
DateFormat d2 = DateFormatgetDateTimeInstance();
String str2 = d2format(now);
DateFormat d3 = DateFormatgetTimeInstance();
String str3 = d3format(now);
DateFormat d4 = DateFormatgetInstance(); //使用SHORT风格显示日期和时间
String str4 = d4format(now);
DateFormat d5 = DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL); //显示日期,周,时间(精确到秒)
String str5 = d5format(now);
DateFormat d6 = DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG); //显示日期。时间(精确到秒)
String str6 = d6format(now);
DateFormat d7 = DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT); //显示日期,时间(精确到分)
String str7 = d7format(now);
DateFormat d8 = DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM); //显示日期,时间(精确到分)
String str8 = d8format(now);//与SHORT风格相比,这种方式最好用
Systemoutprintln("用Date方式显示时间: " + now);//此方法显示的结果和CalendargetInstance()getTime()一样
Systemoutprintln("用DateFormatgetDateInstance()格式化时间后为:" + str1);
Systemoutprintln("用DateFormatgetDateTimeInstance()格式化时间后为:" + str2);
Systemoutprintln("用DateFormatgetTimeInstance()格式化时间后为:" + str3);
Systemoutprintln("用DateFormatgetInstance()格式化时间后为:" + str4);

Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL)格式化时间后为:" + str5);
Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG)格式化时间后为:" + str6);
Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT)格式化时间后为:" + str7);
Systemoutprintln("用DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM)格式化时间后为:" + str8);
}
}
运行结果:
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFormatgetDateInstance()格式化时间后为:2008-6-16
用DateFormatgetDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFormatgetTimeInstance()格式化时间后为:20:54:53
用DateFormatgetInstance()格式化时间后为:08-6-16 下午8:54
用DateFormatgetDateTimeInstance(DateFormatFULL,DateFormatFULL)格式化时间后为
:2008年6月16日 星期一 下午08时54分53秒 CST
用DateFormatgetDateTimeInstance(DateFormatLONG,DateFormatLONG)格式化时间后为
:2008年6月16日 下午08时54分53秒
用DateFormatgetDateTimeInstance(DateFormatSHORT,DateFormatSHORT)格式化时间后
为:08-6-16 下午8:54
用DateFormatgetDateTimeInstance(DateFormatMEDIUM,DateFormatMEDIUM)格式化时间
后为:2008-6-16 20:54:53
方法二:用javautilCalendar类来实现,看下面:
import javautil;
import javatext;
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
public class TestDate2 {
public static void main(String[] args) {

Calendar ca = CalendargetInstance();
int year = caget(CalendarYEAR);//获取年份
int month=caget(CalendarMONTH);//获取月份
int day=caget(CalendarDATE);//获取日
int minute=caget(CalendarMINUTE);//分
int hour=caget(CalendarHOUR);//小时
int second=caget(CalendarSECOND);//秒
int WeekOfYear = caget(CalendarDAY_OF_WEEK);
Systemoutprintln("用CalendargetInstance()getTime()方式显示时间: " + cagetTime());
Systemoutprintln("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");

Systemoutprintln("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");
Systemoutprintln(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)

}
}
运行结果是:
用CalendargetInstance()getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒
2
总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。
还有一种方法利用SystemcurrentTimeMillis()也可以。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13378867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存