教你使用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ū)問(wèn)題會(huì)耗費(fèi)較多的時(shí)間。
到此這篇關(guān)于如何使用Java獲取當(dāng)前時(shí)間戳的文章就介紹到這了,更多相關(guān)Java獲取當(dāng)前時(shí)間戳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(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)文章
spring boot使用@Async異步注解的實(shí)現(xiàn)原理+源碼
通常我們都是采用多線程的方式來(lái)實(shí)現(xiàn)上述業(yè)務(wù)功能,但spring 提供更優(yōu)雅的方式來(lái)實(shí)現(xiàn)上述功能,就是@Async 異步注解,在方法上添加@Async,spring就會(huì)借助AOP,異步執(zhí)行方法,接下來(lái)通過(guò)本文給大家介紹spring boot異步注解的相關(guān)知識(shí),一起看看吧2021-06-06
SpringBoot通過(guò)ip獲取歸屬地的幾種方式分享
在日常我們逛網(wǎng)站的時(shí)候會(huì)發(fā)現(xiàn)我們登錄后會(huì)出現(xiàn)歸屬地信息,例如:我在廣州登錄會(huì)顯示廣東廣州,有些更加精確的會(huì)顯示到區(qū)縣,那么我們來(lái)看看有哪些方式來(lái)獲取歸屬地信息,今天我們來(lái)聊一聊2023-09-09
mybatis-plus?查詢(xún)傳入?yún)?shù)Map,返回List<Map>方式
這篇文章主要介紹了mybatis-plus?查詢(xún)傳入?yún)?shù)Map,返回List<Map>方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法
這篇文章主要介紹了springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
Springboot基于websocket實(shí)現(xiàn)簡(jiǎn)單在線聊天功能
這篇文章主要介紹了Springboot基于websocket實(shí)現(xiàn)簡(jiǎn)單在線聊天功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

