最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

教你使用Java獲取當(dāng)前時(shí)間戳的詳細(xì)代碼

 更新時(shí)間:2022年01月25日 11:11:39   作者:cyan20115  
這篇文章主要介紹了如何使用Java獲取當(dāng)前時(shí)間戳,通過(guò)兩個(gè)java示例,向大家展示如何獲取java中的當(dāng)前時(shí)間戳,文本通過(guò)示例代碼給大家展示了java獲取當(dāng)前時(shí)間戳的方法,需要的朋友可以參考下

要獲取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)

java.sql.Timestamp JavaDoc

java.time.Instant JavaDoc

補(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis別名和settings設(shè)置方式

    MyBatis別名和settings設(shè)置方式

    這篇文章主要介紹了MyBatis別名和settings設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • spring boot使用@Async異步注解的實(shí)現(xià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
  • spring中的FactoryBean代碼示例

    spring中的FactoryBean代碼示例

    這篇文章主要介紹了spring中的FactoryBean代碼示例,涉及FactoryBean的實(shí)現(xiàn)等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Spring事務(wù)管理的使用細(xì)則淺析

    Spring事務(wù)管理的使用細(xì)則淺析

    事務(wù)的作用就是為了保證用戶的每一個(gè)操作都是可靠的,事務(wù)中的每一步操作都必須成功執(zhí)行,只要有發(fā)生異常就 回退到事務(wù)開(kāi)始未進(jìn)行操作的狀態(tài)。事務(wù)管理是Spring框架中最為常用的功能之一,我們?cè)谑褂肧pring開(kāi)發(fā)應(yīng)用時(shí),大部分情況下也都需要使用事務(wù)
    2023-02-02
  • SpringBoot通過(guò)ip獲取歸屬地的幾種方式分享

    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>方式

    這篇文章主要介紹了mybatis-plus?查詢(xún)傳入?yún)?shù)Map,返回List<Map>方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 解決SpringBoot跨域的三種方式

    解決SpringBoot跨域的三種方式

    前后端分離是目前的趨勢(shì),解決跨域問(wèn)題也是老生常談的話題了,我們了解一下什么是域和跨域。域:協(xié)議 + 域名 + 端口;三者完全相同則為同域,反之有其一不同均為不同域。跨域請(qǐng)求:當(dāng)前【發(fā)起請(qǐng)求】的域和【請(qǐng)求指向】的域?qū)儆诓煌驎r(shí),該次請(qǐng)求稱(chēng)之為跨域請(qǐng)求
    2021-06-06
  • 談?wù)勛兞棵?guī)范的重要性

    談?wù)勛兞棵?guī)范的重要性

    下面小編就為大家?guī)?lái)一篇談?wù)勛兞棵?guī)范的重要性。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法

    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)單在線聊天功能

    這篇文章主要介紹了Springboot基于websocket實(shí)現(xiàn)簡(jiǎn)單在線聊天功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06

最新評(píng)論

西安市| 鄂尔多斯市| 武平县| 孙吴县| 汪清县| 宽甸| 隆德县| 兴文县| 扶余县| 沙雅县| 光山县| 土默特右旗| 三原县| 兰溪市| 建水县| 延吉市| 汕头市| 津南区| 邵武市| 临武县| 九龙坡区| 科技| 南通市| 浦县| 永靖县| 嘉兴市| 长沙县| 昌宁县| 星座| 汽车| 迁安市| 左权县| 青龙| 金坛市| 柏乡县| 呼和浩特市| 汤原县| 五常市| 道真| 泸西县| 蕲春县|