獲取系統時間的幾種方法!!!

2021-08-16 17:29:25 字數 1122 閱讀 9440

1.  date day=new date();    

******dateformat df = new ******dateformat("yyyy-mm-dd hh:mm:ss"); 

system.out.println(df.format(day));   

通過date類來獲取當前時間    

2.  date date = new date();    

string year = string.format("%ty", date);   

string month = string.format("%tb", date);   

string day = string.format("%te", date);    

system.out.println("今天是:"+year+"-"+month+"-"+day);   

通過date類來獲取當前時間   

3. ******dateformat df = new ******dateformat("yyyy-mm-dd hh:mm:ss");   system.out.println(df.format(system.currenttimemillis()));   

通過system類中的currenttimemillis方法來獲取當前時間

4.  calendar c = calendar.getinstance();//可以對每個時間域單獨修改   

int year = c.get(calendar.year);  

int month = c.get(calendar.month);   

int date = c.get(calendar.date);    

int hour = c.get(calendar.hour_of_day);   

int minute = c.get(calendar.minute);   

int second = c.get(calendar.second);    

system.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);    

通過calendar類來獲取當前時間

VC 獲取當前系統時間的幾種方法

方案 優點 僅使用c標準庫 缺點 只能精確到秒級 include include int main void 方案二 優點 能精確到毫秒級 缺點 使用了windows api include include int main void 方案三,優點 利用系統函式 include includeusi...

linux中獲取系統時間的幾種方法

asctime 將時間和日期以字串格式表示 相關函式 time,ctime,gmtime,localtime 表頭檔案 include 定義函式 char asctime const struct tm timeptr 函式說明 asctime 將引數timeptr所指的tm結構中的資訊轉換成真實世...

linux中獲取系統時間的幾種方法

asctime 將時間和日期以字串格式表示 相關函式 time,ctime,gmtime,localtime 表頭檔案 include 定義函式 char asctime const struct tm timeptr 函式說明 asctime 將引數timeptr所指的tm結構中的資訊轉換成真實世...