c++时间与java时间相互转化代码实例

来源:互联网 发布:中国软件协会培训中心 编辑:程序博客网 时间:2024/05/15 19:37

最近一直在处理关于时间的一些各种转化,下面贴一些代码。


1 将java下毫秒时间转化为年月日

// s--毫秒时间的string类型     

void timeTransform(std::string s ,int &year,int &month,int &day,int &hour,int &minute)
{

s = s.substr(0, s.length()-3);
long b = atol( s.c_str() );

time_t t;  //秒时间
tm* local = new tm; //本地时间 
t = (time_t)(b) ; //获取目前秒时间
localtime_s( local,&t ); //转为本地时间

year = local->tm_year+ 1900 ;
month = local->tm_mon + 1;
day = local->tm_mday ;
hour = local->tm_hour ;
minute = local->tm_min ;
delete local;
}

2 将 年月日等转化成string类型的毫秒时间(毫秒全部设置成了000)

//返回string类型的毫秒时间

string change_time(int year,int month,int day,int hour,int minute)
{
struct  tm test;
test.tm_hour = hour;
test.tm_mday = day;
test.tm_min = minute;
test.tm_mon = month-1;
test.tm_year = year-1900 ;
test.tm_sec = 0;
time_t t = mktime( &test);


char second[20] ;
_ltoa((long)t, second,10);
string ret = second ;
ret.append("000");
return ret ;
}


3返回当前系统的string类型的毫秒时间

string get_current_time()
{
SYSTEMTIME sys;
GetLocalTime( &sys );
time_t  t;
time( &t );
char second[20] ;
char millSecond[5];
_ltoa((long)t, second,10);
_ltoa((long)sys.wMilliseconds, millSecond,10);


string ret = second ;
ret.append(millSecond);
return ret ;
}

0 0
原创粉丝点击