* 格式化字符串 template < typename . . . Args > std :: string strsprintf ( const char * format , Args . . . args ) { int length = std :: snprintf ( nullptr , 0 , format , args . . . ) ; assert ( length >= 0 ) ; char * buf = new char [ length + 1 ] ; std :: snprintf ( buf , length + 1 , format , args . . . ) ; std :: string str ( buf ) ; delete [ ] buf ; return str ; # ifdef _WIN32 struct timeval { long tv_sec ; /* seconds */ long tv_usec ; /* and microseconds */ inline int gettimeofday ( struct timeval * tp , void * tzp ) { time_t clock ; struct tm tm ; SYSTEMTIME wtm ; GetLocalTime ( & wtm ) ; tm . tm_year = wtm . wYear - 1900 ; tm . tm_mon = wtm . wMonth - 1 ; tm . tm_mday = wtm . wDay ; tm . tm_hour = wtm . wHour ; tm . tm_min = wtm . wMinute ; tm . tm_sec = wtm . wSecond ; tm . tm_isdst = - 1 ; clock = mktime ( & tm ) ; tp - > tv_sec = clock ; tp - > tv_usec = wtm . wMilliseconds * 1000 ; return ( 0 ) ; # endif inline std :: string now2str ( ) { time_t tt = time ( NULL ) ; //这句返回的只是一个时间戳 struct timeval tv ; gettimeofday ( & tv , NULL ) ; tm * t = localtime ( & tt ) ; long milliseconds = tv . tv_usec / 1000 ; char * time_str = new char [ 40 ] ; sprintf ( time_str , "%d-%02d-%02d %02d:%02d:%02d.%03ld" , t - > tm_year + 1900 , t - > tm_mon + 1 , t - > tm_mday , t - > tm_hour , t - > tm_min , t - > tm_sec , milliseconds ) ; //输出格式为: 20200717 105228.302 //delete t; std :: string time_full_str ( time_str ) ; delete [ ] time_str ; return time_full_str ; # endif

这里要特别注意,now2str()返回的是std::string格式,得用std::cout打印,若用printf打印则是乱码的,printf只支持char*打印
在这里插入图片描述

测试方法如下:

int main(void){
	std::string now = now2str();
	std::cout<<"当前时间为"<<now<<std::endl;
	printf( "当前时间=%s\r\n", now.c_str() ):
在使用mybatis,对应不同的数据库 oracle 、mysql等,对于查询到的时间都需要格式化
对于查询得到的时间,可以使用fastjson提供的注解@JsonFormat(“yyyy-MM-dd HH:mm:dd”)来处理
对于以时间为查询条件的,mysql和oracle对时间的处理还不一样。MySQL对于时间字段,可以使用字符串的时间作为条件;Oracle 必须以时间类型作为参数来查询,否则会报错
import java.text.Pa
                                    本文主要向大家介绍了C/C++知识点之C语言关于时间日期格式的设置,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。参 数:%H 小时(以00-23来表示)。%I 小时(以01-12来表示)。%K 小时(以0-23来表示)。%l 小时(以0-12来表示)。%M 分钟(以00-59来表示)。%P AM或PM。%r 时间(含时分秒,小时以12小时AM/PM来表示)。%s 总秒数。起算...
                                    该Intl.RelativeTimeFormat对象是一个构造函数,可以实现本地区域化相对时间格式。犹如Web端我们常见的“昨天”,“20秒前”或“1个月”之类的短语,而不是完整日期和时间戳。Web应用程序通常使用“昨天”,“多少秒前”或“几个月”之类的短语,而不是完整日期和时间戳。这种相对时间格式的值已经变得如此普遍,以至于几个流行的库实现了以局部方式格式化它们的效用函数。(例子包括Moment...