歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java獲取當前系統時間System.currentTimeMillis()

Java獲取當前系統時間System.currentTimeMillis()

日期:2017/3/1 9:26:44   编辑:Linux編程

System.currentTimeMillis()產生一個當前的毫秒,這個毫秒其實就是自1970年1月1日0時起的毫秒數,Date()其實就是相當於Date(System.currentTimeMillis());因為Date類還有構造Date(long date),用來計算long秒與1970年1月1日之間的毫秒差。
得到了這個毫秒數,我們自己也可以算起現在的年月日周時,但是這不是我們去計算的,因為有Calendar。Calendar最終出的結果就是年月日周時時區。

System.currentTimeMillis() 獲得的是自1970-1-01 00:00:00.000 到當前時刻的時間距離,類型為long
String.valueOf(System.currentTimeMillis()) 這個語句可轉為以下的型式:
long ct = System.currentTimeMillis();
String t = String.valueOf(ct);
其實上面的String t就相當於 ct+"";
只是轉為字符串格式

public String refFormatNowDate() {
Date nowTime = new Date(System.currentTimeMillis());
SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
String retStrFormatNowDate = sdFormatter.format(nowTime);

return retStrFormatNowDate;
}

Copyright © Linux教程網 All Rights Reserved