Java System.currentTimeMillis()時(shí)間的單位轉(zhuǎn)換與計(jì)算方式案例詳解
1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)1秒=1,000,000,000 納秒(ns) 1納秒=1/1,000,000,000秒(s)1秒=1,000,000,000,000 皮秒(ps) 1皮秒=1/1,000,000,000,000秒(s)
1分鐘=60秒
1小時(shí)=60分鐘=3600秒
二、System.currentTimeMillis()計(jì)算方式在開發(fā)過程中,通常很多人都習(xí)慣使用new Date()來獲取當(dāng)前時(shí)間。new Date()所做的事情其實(shí)就是調(diào)用了System.currentTimeMillis()。如果僅僅是需要或者毫秒數(shù),那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上會(huì)高一點(diǎn)。如果需要在同一個(gè)方法里面多次使用new Date(),通常性能就是這樣一點(diǎn)一點(diǎn)地消耗掉,這里其實(shí)可以聲明一個(gè)引用。
//獲得系統(tǒng)的時(shí)間,單位為毫秒,轉(zhuǎn)換為妙long totalMilliSeconds = System.currentTimeMillis();long totalSeconds = totalMilliSeconds / 1000; //求出現(xiàn)在的秒long currentSecond = totalSeconds % 60; //求出現(xiàn)在的分long totalMinutes = totalSeconds / 60;long currentMinute = totalMinutes % 60; //求出現(xiàn)在的小時(shí)long totalHour = totalMinutes / 60;long currentHour = totalHour % 24; //顯示時(shí)間System.out.println('總毫秒為: ' + totalMilliSeconds);System.out.println(currentHour + ':' + currentMinute + ':' + currentSecond + ' GMT');
小例子:
package demo.spli;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.TimeZone;public class ShowCurrentTime { /** * @顯示當(dāng)前時(shí)間 * @2014.9.3 */ public static void main(String[] args) {// TODO Auto-generated method stub//獲得系統(tǒng)的時(shí)間,單位為毫秒,轉(zhuǎn)換為妙long totalMilliSeconds = System.currentTimeMillis();DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化輸出TimeZone timeZoneChina = TimeZone.getTimeZone('Asia/Shanghai');//獲取時(shí)區(qū) 這句加上,很關(guān)鍵。dateFormatterChina.setTimeZone(timeZoneChina);//設(shè)置系統(tǒng)時(shí)區(qū)long totalSeconds = totalMilliSeconds / 1000;//求出現(xiàn)在的秒long currentSecond = totalSeconds % 60;//求出現(xiàn)在的分long totalMinutes = totalSeconds / 60;long currentMinute = totalMinutes % 60;//求出現(xiàn)在的小時(shí)long totalHour = totalMinutes / 60;long currentHour = totalHour % 24;//顯示時(shí)間System.out.println('總毫秒為: ' + totalMilliSeconds);System.out.println(currentHour + ':' + currentMinute + ':' + currentSecond + ' GMT');Date nowTime = new Date(System.currentTimeMillis());System.out.println(System.currentTimeMillis());SimpleDateFormat sdFormatter = new SimpleDateFormat('yyyy-MM-dd HH:mm:dd');String retStrFormatNowDate = sdFormatter.format(nowTime); System.out.println(retStrFormatNowDate); }}
System.currentTimeMillis()+3600*1000)可以這樣解讀:System.currentTimeMillis()相當(dāng)于是毫秒為單位,但是,后頭成了1000,就變成了以秒為單位。那么,3600秒=1小時(shí),所以輸出為當(dāng)前時(shí)間的1小時(shí)后。
我們可以這樣控制時(shí)間:System.currentTimeMillis()+time*1000),里面?zhèn)魅氲膖ime是以秒為單位,當(dāng)傳入60,則輸出:當(dāng)前時(shí)間的一分鐘后
到此這篇關(guān)于Java System.currentTimeMillis()時(shí)間的單位轉(zhuǎn)換與計(jì)算方式案例詳解的文章就介紹到這了,更多相關(guān)Java System.currentTimeMillis()操作內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解2. 淺談python出錯(cuò)時(shí)traceback的解讀3. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼4. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解5. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. Nginx+php配置文件及原理解析8. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法9. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
