教你使用Java獲取當前時間戳的詳細代碼
要獲取Java中的當前時間戳:
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); //2016-11-16 06:43:19.77
這是兩個Java示例,向您展示如何獲取Java中的當前時間戳。 (使用Java 8更新)
1. java.sql.Timestamp
獲得當前java.sql.Timestamp
兩種方法
TimeStampExample.java
package com.mkyong.date; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; public class TimeStampExample { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); public static void main(String[] args) { //method 1 Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(timestamp); //method 2 - via Date Date date = new Date(); System.out.println(new Timestamp(date.getTime())); //return number of milliseconds since January 1, 1970, 00:00:00 GMT System.out.println(timestamp.getTime()); //format timestamp System.out.println(sdf.format(timestamp)); } }
輸出量
2016-11-16 06:43:19.77
2016-11-16 06:43:19.769
1479249799770
2016.11.16.06.43.19
2. java.time.Instant
在Java 8中,可以將java.sql.Timestamp
轉(zhuǎn)換為新的java.time.Instant
InstantExample.java
package com.mkyong.date; import java.sql.Timestamp; import java.time.Instant; public class InstantExample { public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(timestamp); //return number of milliseconds since January 1, 1970, 00:00:00 GMT System.out.println(timestamp.getTime()); // Convert timestamp to instant Instant instant = timestamp.toInstant(); System.out.println(instant); //return number of milliseconds since the epoch of 1970-01-01T00:00:00Z System.out.println(instant.toEpochMilli()); // Convert instant to timestamp Timestamp tsFromInstant = Timestamp.from(instant); System.out.println(tsFromInstant.getTime()); } }
輸出量
2016-11-16 06:55:40.11
1479250540110
2016-11-15T22:55:40.110Z
1479250540110
1479250540110
參考文獻
補充:java獲取當前時間戳的方法
獲取當前時間戳
//方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方法 三 new Date().getTime();
獲取當前時間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式 String date = df.format(new Date());// new Date()為獲取當前系統(tǒng)時間,也可使用當前時間戳
獲取時間戳三種方法執(zhí)行效率比較:
import java.util.Calendar; import java.util.Date; ? public class TimeTest { ? ? private static long _TEN_THOUSAND=10000; ? ? public static void main(String[] args) { ? ? ? ? long times=1000*_TEN_THOUSAND; ? ? ? ? long t1=System.currentTimeMillis(); ? ? ? ? testSystem(times); ? ? ? ? long t2=System.currentTimeMillis(); ? ? ? ? System.out.println(t2-t1); ? ? ? ? testCalander(times); ? ? ? ? long t3=System.currentTimeMillis(); ? ? ? ? System.out.println(t3-t2); ? ? ? ? testDate(times); ? ? ? ? long t4=System.currentTimeMillis(); ? ? ? ? System.out.println(t4-t3); ? ? } ? ? public static void testSystem(long times){//use 188 ? ? ? ? for(int i=0;i<times;i++){ ? ? ? ? ? ? long currentTime=System.currentTimeMillis(); ? ? ? ? } ? ? public static void testCalander(long times){//use 6299 ? ? ? ? ? ? long currentTime=Calendar.getInstance().getTimeInMillis(); ? ? public static void testDate(long times){ ? ? ? ? ? ? long currentTime=new Date().getTime(); }
執(zhí)行結(jié)果:
133
2372
137
Calendar.getInstance().getTimeInMillis() 這種方式速度最慢,這是因為Canlendar要處理時區(qū)問題會耗費較多的時間。
到此這篇關(guān)于如何使用Java獲取當前時間戳的文章就介紹到這了,更多相關(guān)Java獲取當前時間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring-boot-maven-plugin:unknown的完美解決方法
這篇文章主要介紹了spring-boot-maven-plugin:unknown的完美解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11一步步教你搭建Scala開發(fā)環(huán)境(非常詳細!)
Scala是一門基于jvm的函數(shù)式的面向?qū)ο缶幊陶Z言,擁有比java更加簡潔的語法,下面這篇文章主要給大家介紹了關(guān)于搭建Scala開發(fā)環(huán)境的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-04-04Java使用POI導出大數(shù)據(jù)量Excel的方法
今天需要寫一個導出的Excel的功能,但是發(fā)現(xiàn)當數(shù)據(jù)量到3萬條時,列數(shù)在23列時,內(nèi)存溢出,CPU使用100%,測試環(huán)境直接炸掉。小編給大家分享基于java使用POI導出大數(shù)據(jù)量Excel的方法,感興趣的朋友一起看看吧2019-11-11