Java System.currentTimeMillis()時間的單位轉換與計算方式案例詳解
一、時間的單位轉換
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小時=60分鐘=3600秒
二、System.currentTimeMillis()計算方式
在開發(fā)過程中,通常很多人都習慣使用new Date()來獲取當前時間。new Date()所做的事情其實就是調用了System.currentTimeMillis()。如果僅僅是需要或者毫秒數(shù),那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上會高一點。如果需要在同一個方法里面多次使用new Date(),通常性能就是這樣一點一點地消耗掉,這里其實可以聲明一個引用。
//獲得系統(tǒng)的時間,單位為毫秒,轉換為妙
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)在的小時
long totalHour = totalMinutes / 60;
long currentHour = totalHour % 24;
//顯示時間
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 {
/**
* @顯示當前時間
* @2014.9.3
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//獲得系統(tǒng)的時間,單位為毫秒,轉換為妙
long totalMilliSeconds = System.currentTimeMillis();
DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化輸出
TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//獲取時區(qū) 這句加上,很關鍵。
dateFormatterChina.setTimeZone(timeZoneChina);//設置系統(tǒng)時區(qū)
long totalSeconds = totalMilliSeconds / 1000;
//求出現(xiàn)在的秒
long currentSecond = totalSeconds % 60;
//求出現(xiàn)在的分
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
//求出現(xiàn)在的小時
long totalHour = totalMinutes / 60;
long currentHour = totalHour % 24;
//顯示時間
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()相當于是毫秒為單位,但是,后頭成了1000,就變成了以秒為單位。那么,3600秒=1小時,所以輸出為當前時間的1小時后。
我們可以這樣控制時間:System.currentTimeMillis()+time*1000),里面?zhèn)魅氲膖ime是以秒為單位,當傳入60,則輸出:當前時間的一分鐘后
到此這篇關于Java System.currentTimeMillis()時間的單位轉換與計算方式案例詳解的文章就介紹到這了,更多相關Java System.currentTimeMillis()操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java?Web項目中如何添加Tomcat的Servlet-api.jar包(基于IDEA)
servlet-api.jar是在編寫servlet必須用到的jar包下面這篇文章主要給大家介紹了基于IDEAJava?Web項目中如何添加Tomcat的Servlet-api.jar包的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-04-04
SpringBoot?@GroupSequenceProvider注解實現(xiàn)bean多屬性聯(lián)合校驗的示例代碼
這篇文章主要介紹了SpringBoot?@GroupSequenceProvider注解實現(xiàn)bean多屬性聯(lián)合校驗,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
springboot mybatis druid配置多數(shù)據(jù)源教程
這篇文章主要介紹了springboot mybatis druid配置多數(shù)據(jù)源教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
獲取Java的MyBatis框架項目中的SqlSession的方法
SqlSession中包括已經(jīng)映射好的SQL語句,這樣對象實例就可以直接拿過來用了,那么這里就來講解獲取Java的MyBatis框架項目中的SqlSession的方法2016-06-06
Java并發(fā)編程包中atomic的實現(xiàn)原理示例詳解
這篇文章主要給大家介紹了關于Java并發(fā)編程包中atomic的實現(xiàn)原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09

