黄色网站入口国产美女,精品国产欧美另类一区,国产一区二区美女自慰,日日摸夜夜添无码国产

選擇你喜歡的標(biāo)簽
我們會(huì)為你匹配適合你的網(wǎng)址導(dǎo)航

    確認(rèn) 跳過

    跳過將刪除所有初始化信息

    您的位置:0XUCN > 資訊 > 技術(shù)
    新聞分類

    Java獲取當(dāng)前時(shí)間的幾種方法

    技術(shù) PRO 作者:程念慈 2021-05-27 18:33

    開發(fā)過程中,可能會(huì)遇到精確的時(shí)間差值計(jì)算,可以使用System.nanoTime();方法獲取當(dāng)前系統(tǒng)的納秒值。不過該方法只能用于測(cè)量經(jīng)過時(shí)間,與系統(tǒng)時(shí)間的任何其他概念無(wú)關(guān)。用途:計(jì)算時(shí)間差,當(dāng)隨機(jī)數(shù)來(lái)使用(不建議)。

    獲取當(dāng)前時(shí)間的毫秒值:System.currentTimeMillis();返回long型。

    通過java.util.Date實(shí)例中的getTime()方法獲取當(dāng)前時(shí)間的毫秒值(從1970年1月1日算起),此方法在開發(fā)中經(jīng)常被使用。如果只是為了獲取當(dāng)前時(shí)間的毫秒值,其實(shí)是沒必要這么做的,直接使用System.currentTimeMillis()即可獲取。下圖Java源碼中,當(dāng)實(shí)例化一個(gè)Date對(duì)象的時(shí)候,其實(shí)使用的就是System.currentTimeMillis(),接著使用getTime()再讓JVM去計(jì)算一遍獲取當(dāng)前毫秒值實(shí)在是多次一舉,耗費(fèi)了性能。

    Instant:JAVA8新特性,獲取當(dāng)前瞬間的實(shí)例。如果想要獲取更精確的納秒級(jí)時(shí)間值,可以使用Instant對(duì)象。Instant中不提供公共的構(gòu)造方法,因此只能使用靜態(tài)方法獲取Instant實(shí)例。簡(jiǎn)單介紹一下Instant對(duì)象中的常用方法。

    1. Instant.now();獲取當(dāng)前時(shí)間的瞬間實(shí)例,靜態(tài)方法,返回Instant類型
    2. getEpochSecond();得到瞬間實(shí)例的秒值
    3. toEpochMilli();得到瞬間實(shí)例的毫秒值
    4. getNano();得到瞬間實(shí)例的納秒值
    5. isAfter(Instant instant);比較這個(gè)瞬間是否在指定的時(shí)刻之后
    6. isBefore(Instant instant);比較這個(gè)瞬間是否在指定的時(shí)刻之前
    7. equals(Instant instant);檢查這個(gè)瞬間是否等于指定的時(shí)刻

    代碼

    ????//該方法只能用于測(cè)量經(jīng)過時(shí)間,與系統(tǒng)時(shí)間的任何其他概念無(wú)關(guān)。用途:計(jì)算時(shí)間差,當(dāng)隨機(jī)數(shù)來(lái)使用(不建議)

    ????System.out.println(System.nanoTime());//47307604336560
    ????//獲取系統(tǒng)當(dāng)前時(shí)間的毫秒值,從1970 年 1 月 1 日起計(jì)算
    ????System.out.println(System.currentTimeMillis());//1541509585320
    ????//獲取系統(tǒng)當(dāng)前時(shí)間的毫秒值,不建議這么使用.建議使用上面的方法.
    ????System.out.println(new Date().getTime());//1541509585322
    ?????
    ????//獲取當(dāng)前瞬間的實(shí)例,JAVA8新特性
    ????Instant instant = Instant.now();
    ????System.out.println(instant);//2018-11-06T13:06:25.353Z
    ????//得到當(dāng)前瞬間的秒數(shù)
    ????System.out.println(instant.getEpochSecond());//1541509585
    ????//得到當(dāng)前瞬間的毫秒數(shù)
    ????System.out.println(instant.toEpochMilli());//1541509585353
    ????//得到當(dāng)前瞬間的納秒數(shù)
    ????System.out.println(instant.getNano());//353000000
    ?????
    ????Instant instantA= Instant.now();
    ????//比較兩個(gè)instant的前后關(guān)系,返回boolean類型
    ????System.out.println(instant.isAfter(instantA));//false
    ????System.out.println(instant.isBefore(instantA));//true
    ????//比較兩個(gè)instant是否相等
    ????System.out.println(instant.equals(instantA));//false
    

    0XU.CN

    [超站]友情鏈接:

    四季很好,只要有你,文娛排行榜:https://www.yaopaiming.com/
    關(guān)注數(shù)據(jù)與安全,洞悉企業(yè)級(jí)服務(wù)市場(chǎng):https://www.ijiandao.com/

    圖庫(kù)
    公眾號(hào) 關(guān)注網(wǎng)絡(luò)尖刀微信公眾號(hào)
    隨時(shí)掌握互聯(lián)網(wǎng)精彩
    贊助鏈接