[C++]调用系统时间

今天突然想写一个时钟,当然理想是很美好的,希望做成数字和模拟的两种类型,于是想到了调用系统时钟的是想方法,于是有了此文

C++中对于系统时间的调用:

#include<time.h> 
#include<iostream.h>
int main(void) 
{
 time_t now;
 struct tm *timenow;
 time(&now); 
 //time函数读取现在的时间(国际标准时间非北京时间),然后传值给now 
 timenow = localtime(&now); 
 //localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区) 
 cout<<"Local time is "<<asctime(timenow)<<endl; 
 //上句中asctime函数把时间转换成字符输出 
 return 0;
}

PS:

1.关于C中的系统时间调用

1 使用time_t time( time_t * timer ) 精确到秒
2 使用clock_t clock() 得到的是CPU时间 精确到1/CLOCKS_PER_SEC秒
3 计算时间差使用double difftime( time_t timer1, time_t timer0 )
4 使用DWORD GetTickCount() 精确到毫秒
5 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒
6 要获取高精度时间,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
获取系统的计数器的频率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
获取计数器的值
然后用两次计数器的差除以Frequency就得到时间。
7 Multimedia Timer Functions
The following functions are used with multimedia timers.
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime

2.关于java中的系统时间调用

一. 获取当前系统时间和日期并格式化输出:

 

import java.util.Date;
import java.text.SimpleDateFormat;

public class NowString 
{
  public static void main(String[] args) 
  { 
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
  }
}



二. 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:


1、用convert()转化函数:

String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

System.out.println(rs.getString("convertBookDate"));


2、利用SimpleDateFormat类:


先要输入两个java包:

 

import java.util.Date;
import java.text.SimpleDateFormat;


然后:


定义日期格式:

 

SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);


sql语句为:

 

String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";


输出:

System.out.println(df.format(rs.getDate("bookDate")));
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值