亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java中時間戳和時間的轉(zhuǎn)換方法代碼

 更新時間:2025年03月25日 08:28:44   作者:思思文  
這篇文章主要介紹了Java中時間戳和時間的轉(zhuǎn)換的相關(guān)資料,Java8中時間戳與日期時間對象之間的轉(zhuǎn)換是編程中常見的操作,通過時間字符串獲取時間對象也是其中的一種方法,需要的朋友可以參考下

前言

不論是什么編程語言,無論是剛開始學(xué)習(xí),還是做了多長時間的猿,都離不開時間戳和時間的轉(zhuǎn)換。Java中也是這樣,現(xiàn)在接觸到最低的java版本是Java8.其中的時間

獲取時間戳

//使用系統(tǒng)時間戳
long timestamp = System.currentTimeMillis();
System.out.println("timestamp:" + timestamp);
//>>timestamp:1733907943319

Long timestamp1 = Instant.now().toEpochMilli();
System.out.println("timestamp1:"+timestamp1);
//>>1733908000856

//獲取秒級時間戳
long timestamp2 = System.currentTimeMillis() / 1000;
System.out.println("timestamp2:"+timestamp2);
//>>1733908113

long timestamp3 = Instant.now().getEpochSecond();
System.out.println("timestamp3:"+timestamp3);
//>>1733908113

日期時間對象轉(zhuǎn)時間戳

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime:"+localDateTime);
//>>localDateTime:2024-12-11T17:10:45.800
//獲取我們常見的時間格式
String dateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("dateTime:"+dateTime);
//>>dateTime:2023-04-07 14:06:53
//localDateTime獲取時間戳
long timestamp4 = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println("timestamp4:"+timestamp4);
//>>1733908245800
long timestamp5 = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println("timestamp5:"+timestamp5);
//>> 1733908324740
long timestamp6 = ZonedDateTime.now().toInstant().toEpochMilli();
System.out.println("timestamp6:"+timestamp6);
//>>1733908374866
long timestamp7 = ZonedDateTime.now().toEpochSecond();
System.out.println("timestamp7:"+timestamp7);
//>>1733908374
long timestamp8 = ZonedDateTime.now().toLocalDateTime().toEpochSecond(ZoneOffset.of("+8"));
System.out.println("timestamp8:"+timestamp8);

時間戳轉(zhuǎn)時間對象

long time = System.currentTimeMillis();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 System.out.println("time:"+time);
 // 將毫秒時間戳轉(zhuǎn)換為 Instant 對象
Instant instant=Instant.ofEpochMilli(time);
System.out.println("instant:"+instant);
 // 將 Instant 對象轉(zhuǎn)換為 LocalDateTime 對象,使用系統(tǒng)默認(rèn)時區(qū)
LocalDateTime localDateTime=LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println("localDateTime:"+localDateTime.format(formatter));
LocalDateTime localDateTime1 =LocalDateTime.ofEpochSecond(instant.getEpochSecond(),0, ZoneOffset.of("+8"));
System.out.println("localDateTime1:"+localDateTime1.format(formatter));

通過時間字符串獲取時間對象

String dt = "2024-12-11";
String dtt = "2024-12-11 17:44:28";
String format_DateTime = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter df = DateTimeFormatter.ofPattern(format_DateTime);
LocalDateTime localDateTime = LocalDateTime.parse(dtt, df);
System.out.println("localDateTime:"+localDateTime);
//轉(zhuǎn)為Instant
Instant instant = localDateTime.toInstant(ZoneOffset.of("+8"));
System.out.println("instant:"+instant);
System.out.println("毫秒級時間戳:"+instant.toEpochMilli());
System.out.println("秒級時間戳:"+instant.getEpochSecond());
LocalDate localDate = LocalDate.parse(dt);
System.out.println("localDate:"+localDate);
System.out.println("localDate.atStartOfDay():"+localDate.atStartOfDay().format(df));
//轉(zhuǎn)為Instant
Instant instant1 = localDate.atStartOfDay().toInstant(ZoneOffset.of("+8"));
System.out.println("毫秒級時間戳:"+instant.toEpochMilli());
System.out.println("秒級時間戳:"+instant.getEpochSecond());

以上就是java8中獲取時間戳以及通過實(shí)踐對象獲取時間戳和通過時間戳獲取時間對象。

總結(jié)

到此這篇關(guān)于Java中時間戳和時間的轉(zhuǎn)換方法的文章就介紹到這了,更多相關(guān)Java時間戳和時間轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java反射機(jī)制(Reflection)淺析

    Java反射機(jī)制(Reflection)淺析

    這篇文章主要介紹了Java反射機(jī)制(Reflection)淺析,本文以實(shí)例講解Java的反射機(jī)制,需要的朋友可以參考下
    2014-07-07
  • Spring中的PathVariable注釋解析

    Spring中的PathVariable注釋解析

    這篇文章主要介紹了Spring中的PathVariable注釋用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Eclipse使用maven搭建spring mvc圖文教程

    Eclipse使用maven搭建spring mvc圖文教程

    這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下
    2016-05-05
  • springboot連接kafka集群的使用示例

    springboot連接kafka集群的使用示例

    在項(xiàng)目中使用kafka的場景有很多,尤其是實(shí)時產(chǎn)生的數(shù)據(jù)流,本文主要介紹了springboot連接kafka集群的使用示例,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • 圖解Java經(jīng)典算法希爾排序的原理與實(shí)現(xiàn)

    圖解Java經(jīng)典算法希爾排序的原理與實(shí)現(xiàn)

    希爾排序是希爾(Donald Shell)于1959年提出的一種排序算法。希爾排序也是一種插入排序,它是簡單插入排序經(jīng)過改進(jìn)之后的一個更高效的版本,也稱為縮小增量排序,同時該算法是沖破O(n2)的第一批算法之一。本文會以圖解的方式詳細(xì)介紹希爾排序的基本思想及其代碼實(shí)現(xiàn)
    2022-09-09
  • java多線程編程技術(shù)詳解和實(shí)例代碼

    java多線程編程技術(shù)詳解和實(shí)例代碼

    這篇文章主要介紹了 java多線程編程技術(shù)詳解和實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Java數(shù)據(jù)結(jié)構(gòu)順序表的詳細(xì)講解

    Java數(shù)據(jù)結(jié)構(gòu)順序表的詳細(xì)講解

    大家好,今天給大家?guī)淼氖琼樞虮?,我覺得順序表還是有比較難理解的地方的,于是我就把這一塊的內(nèi)容全部整理到了一起,希望能夠給剛剛進(jìn)行學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)的人帶來一些幫助,或者是已經(jīng)學(xué)過這塊的朋友們帶來更深的理解,我們現(xiàn)在就開始吧
    2022-05-05
  • 詳解Spring Retry實(shí)現(xiàn)原理

    詳解Spring Retry實(shí)現(xiàn)原理

    這篇文章主要介紹了詳解Spring Retry實(shí)現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動態(tài)維護(hù)的示例代碼

    spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動態(tài)維護(hù)的示例代碼

    本文主要介紹了spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動態(tài)維護(hù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • 關(guān)于Maven parent.relativePath說明

    關(guān)于Maven parent.relativePath說明

    Maven中的relativePath用于指定父項(xiàng)目pom.xml的相對路徑,默認(rèn)值為../pom.xml,這個配置幫助Maven在構(gòu)建時定位父模塊的位置,確保模塊間的依賴關(guān)系正確,relativePath可以指向本地或遠(yuǎn)程倉庫中的父項(xiàng)目,如果不需要尋找父項(xiàng)目,可以將其設(shè)置為空
    2024-09-09

最新評論