教你使用Java獲取當(dāng)前時(shí)間戳的詳細(xì)代碼
要獲取Java中的當(dāng)前時(shí)間戳:
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); //2016-11-16 06:43:19.77
這是兩個(gè)Java示例,向您展示如何獲取Java中的當(dāng)前時(shí)間戳。 (使用Java 8更新)
1. java.sql.Timestamp
獲得當(dāng)前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
參考文獻(xiàn)
補(bǔ)充:java獲取當(dāng)前時(shí)間戳的方法
獲取當(dāng)前時(shí)間戳
//方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方法 三 new Date().getTime();
獲取當(dāng)前時(shí)間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式
String date = df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時(shí)間,也可使用當(dāng)前時(shí)間戳獲取時(shí)間戳三種方法執(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() 這種方式速度最慢,這是因?yàn)镃anlendar要處理時(shí)區(qū)問題會(huì)耗費(fèi)較多的時(shí)間。
到此這篇關(guān)于如何使用Java獲取當(dāng)前時(shí)間戳的文章就介紹到這了,更多相關(guān)Java獲取當(dāng)前時(shí)間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java怎么獲取當(dāng)前時(shí)間、計(jì)算程序運(yùn)行時(shí)間源碼詳解(超詳細(xì)!)
- Java獲取當(dāng)前時(shí)間的時(shí)間戳方法總結(jié)
- Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式
- Java獲取時(shí)間如何將當(dāng)前時(shí)間減一天、一月、一年、并格式化
- Java獲取當(dāng)前時(shí)間的時(shí)間戳(13位和10位)
- java獲取當(dāng)前時(shí)間戳的方法
- Java如何獲取當(dāng)前時(shí)間的小時(shí)/分鐘(實(shí)現(xiàn)方法)
相關(guān)文章
java自己手動(dòng)控制kafka的offset操作
這篇文章主要介紹了java自己手動(dòng)控制kafka的offset操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
spring-boot-maven-plugin:unknown的完美解決方法
這篇文章主要介紹了spring-boot-maven-plugin:unknown的完美解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Java Grpc實(shí)例創(chuàng)建負(fù)載均衡詳解
這篇文章主要介紹了Java Grpc實(shí)例創(chuàng)建負(fù)載均衡詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
一步步教你搭建Scala開發(fā)環(huán)境(非常詳細(xì)!)
Scala是一門基于jvm的函數(shù)式的面向?qū)ο缶幊陶Z言,擁有比java更加簡潔的語法,下面這篇文章主要給大家介紹了關(guān)于搭建Scala開發(fā)環(huán)境的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法
今天需要寫一個(gè)導(dǎo)出的Excel的功能,但是發(fā)現(xiàn)當(dāng)數(shù)據(jù)量到3萬條時(shí),列數(shù)在23列時(shí),內(nèi)存溢出,CPU使用100%,測試環(huán)境直接炸掉。小編給大家分享基于java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法,感興趣的朋友一起看看吧2019-11-11

