java 獲取系統時間

2021-08-23 14:09:18 字數 1131 閱讀 2874

1.date day=new date();    

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

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

通過date類來獲取當前時間    

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

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

3.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類來獲取當前時間   

4. 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類來獲取當前時間   

參考:

Java獲取系統日期時間

方法一 利用calendar類來獲取當前日期和時間,如下 獲取系統時間 return public string getdate 方法二 利用date類來獲取當前日期時間,並通過 dateformat類來格式化輸出,如下 獲取當前系統時間 return public string getdate 上...

獲取系統時間

include include include include include include sockaddr in標頭檔案 include include include int main int argc,char argv sock fd socket af inet,sock stream...

獲取系統時間

一,如果只是單純的獲取系統時間,用 dateformat如下 dateformat df new dateformat yyyy mm dd hh mm ss string systime df.format new date system.out.println systime 二,如果有 對年,...