五種方法獲取當前時間戳
摘要:分享獲取當前時間戳的五種方法。例如,通過系統時間System.currentTimeMillis() 或日期類Instant.now().toEpochMilli()來獲取當前時間戳。
什么是時間戳
??在Java語言中,時間戳(timestamp)是一種重要的概念,它是指一個能夠表示某個時間點的單一數字,用于精確記錄和計算時間,單位是毫秒,類型是long。它通常表示從固定的起始時間點(1970-01-01 00:00:00 +0:00,UTC時區)開始到另一個時間點所經過的毫秒數。溫馨提示,一秒等于一千毫秒。
??使用場景請參考《System.currentTimeMillis()與時區無關》。在Java編程中,有哪些途徑可以獲得時間戳呢?小編樓蘭胡楊在本文分享五種方法,請君參考。例如,可以通過系統時間System.currentTimeMillis() 或日期類Instant.now().toEpochMilli()來獲取與時區無關的當前時間戳。
使用系統時間
long time = System.currentTimeMillis();
這是最常用的一種方式。
使用日期類Date
long timestamp = new Date().getTime();
System.out.println("當前時間戳為:" + timestamp);
??由于new Date()的底層實現如下,故不推薦使用這種方案獲取時間戳:
public Date() {
this(System.currentTimeMillis());
}
Calendar類
??使用Calendar類獲取時間戳:
long milliTime = Calendar.getInstance().getTimeInMillis();
使用 Instant 獲取當前時間戳
??使用Java 8 新引入的日期和時間API(java.time)中的Instant類獲取時間戳:
Instant instant = Instant.now();
System.out.println("instant now :" + instant);
// 獲取毫秒
long instantMilli = instant.toEpochMilli();
System.out.println("instant 毫秒:"+instantMilli);
??Instant代表時間線中的一個獨特點,主要用于記錄應用程序中事件的時間戳。它是一個實際的時間節點,使用UTC時區表示,不受系統服務器時區設置的影響。
使用ZonedDateTime
??ZonedDateTime是一個特定時區的時間點,此實現方案已經指定為UTC時區,所以,獲取的時間戳和System.currentTimeMillis()相等,而且,受服務器時區設置的影響。下面先指定時區再獲取時間戳:
public static final ZoneOffset TIME_ZONE_OFF = ZoneOffset.of("Z");
public static Long getZonedNow() {
// return now
return ZonedDateTime.now(TIME_ZONE_OFF).toInstant().toEpochMilli();
}
效率分析
??我們通過比對計算一千萬次時間戳的耗時,來分析哪種方案效率最高,實驗源碼如下:
public class TimeTest {
public static void main(String[] args) {
// 運行一千萬次
long times = 10000000;
long t1 = System.nanoTime();
testSystem(times);
long t2 = System.nanoTime();
System.out.println(t2 - t1);
testDate(times);
long t3 = System.nanoTime();
System.out.println(t3 - t2);
testCalander(times);
long t4 = System.nanoTime();
System.out.println(t4 - t3);
testInstant(times);
long t5 = System.nanoTime();
System.out.println("testInstant耗時:" + (t5 - t4));
testZone(times);
long t6 = System.nanoTime();
System.out.println("testZone耗時:" + (t6 - t5));
}
public static void testSystem(long times) {
for (int i = 0; i < times; i++) {
long currentTime = System.currentTimeMillis();
}
}
public static void testCalander(long times) {
for (int i = 0; i < times; i++) {
long currentTime = Calendar.getInstance().getTimeInMillis();
}
}
public static void testDate(long times) {
for (int i = 0; i < times; i++) {
long currentTime = new Date().getTime();
}
}
public static void testInstant(long times) {
for (int i = 0; i < times; i++) {
long currentTime = Instant.now().toEpochMilli();
}
}
public static void testZone(long times) {
for (int i = 0; i < times; i++) {
long currentTime = getZonedNow();
}
}
public static final ZoneOffset TIME_ZONE_OFF = ZoneOffset.of("Z");
public static Long getZonedNow() {
// return now
return ZonedDateTime.now(TIME_ZONE_OFF).toInstant().toEpochMilli();
}
}
??執行后控制臺打印結果如下:
272071959
259144708
1760647333
testInstant耗時:344117125
testZone耗時:857025542
??各位童鞋可以多執行幾次,看看效果是否一致。由實驗結果可知,System.currentTimeMillis()的效率非常優秀,Calendar.getInstance().getTimeInMillis() 效率最低,這是因為Canlendar在處理時區問題時耗費較多的時間。但是,System.currentTimeMillis()在高并發場景下存在瑕疵,欲知后事如何,且聽下回《System.currentTimeMillis()性能優化》分解。
結束語
??在開發過程中,獲取日期的當前時間戳是Java編程一個非?;镜牟僮?,無論是在日常學習中還是在企業級應用程序開發中,都有著廣泛的應用。使用系統時間和日期類都可以輕松獲取當前時間戳,但需要注意時區和計算機時間的準確性。
??題外話,分享一句意味深長而又富含深意的話:當你身邊的人遮遮掩掩的時候,你可以靜觀其變,看看他們背后在干嘛。若是他們不夠檢點,你應當靜悄悄地保持距離;否則,你就變成了“知情不報”,卷入是非了。
Buy me a coffee. ?Get red packets.
浙公網安備 33010602011771號