Java中時(shí)間戳和時(shí)間的轉(zhuǎn)換方法代碼
前言
不論是什么編程語(yǔ)言,無(wú)論是剛開(kāi)始學(xué)習(xí),還是做了多長(zhǎng)時(shí)間的猿,都離不開(kāi)時(shí)間戳和時(shí)間的轉(zhuǎn)換。Java中也是這樣,現(xiàn)在接觸到最低的java版本是Java8.其中的時(shí)間
獲取時(shí)間戳
//使用系統(tǒng)時(shí)間戳 long timestamp = System.currentTimeMillis(); System.out.println("timestamp:" + timestamp); //>>timestamp:1733907943319 Long timestamp1 = Instant.now().toEpochMilli(); System.out.println("timestamp1:"+timestamp1); //>>1733908000856 //獲取秒級(jí)時(shí)間戳 long timestamp2 = System.currentTimeMillis() / 1000; System.out.println("timestamp2:"+timestamp2); //>>1733908113 long timestamp3 = Instant.now().getEpochSecond(); System.out.println("timestamp3:"+timestamp3); //>>1733908113
日期時(shí)間對(duì)象轉(zhuǎn)時(shí)間戳
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime:"+localDateTime); //>>localDateTime:2024-12-11T17:10:45.800 //獲取我們常見(jiàn)的時(shí)間格式 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獲取時(shí)間戳 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);
時(shí)間戳轉(zhuǎn)時(shí)間對(duì)象
long time = System.currentTimeMillis(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println("time:"+time); // 將毫秒時(shí)間戳轉(zhuǎn)換為 Instant 對(duì)象 Instant instant=Instant.ofEpochMilli(time); System.out.println("instant:"+instant); // 將 Instant 對(duì)象轉(zhuǎn)換為 LocalDateTime 對(duì)象,使用系統(tǒng)默認(rèn)時(shí)區(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));
通過(guò)時(shí)間字符串獲取時(shí)間對(duì)象
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("毫秒級(jí)時(shí)間戳:"+instant.toEpochMilli()); System.out.println("秒級(jí)時(shí)間戳:"+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("毫秒級(jí)時(shí)間戳:"+instant.toEpochMilli()); System.out.println("秒級(jí)時(shí)間戳:"+instant.getEpochSecond());
以上就是java8中獲取時(shí)間戳以及通過(guò)實(shí)踐對(duì)象獲取時(shí)間戳和通過(guò)時(shí)間戳獲取時(shí)間對(duì)象。
總結(jié)
到此這篇關(guān)于Java中時(shí)間戳和時(shí)間的轉(zhuǎn)換方法的文章就介紹到這了,更多相關(guān)Java時(shí)間戳和時(shí)間轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Eclipse使用maven搭建spring mvc圖文教程
這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下2016-05-05圖解Java經(jīng)典算法希爾排序的原理與實(shí)現(xiàn)
希爾排序是希爾(Donald Shell)于1959年提出的一種排序算法。希爾排序也是一種插入排序,它是簡(jiǎn)單插入排序經(jīng)過(guò)改進(jìn)之后的一個(gè)更高效的版本,也稱(chēng)為縮小增量排序,同時(shí)該算法是沖破O(n2)的第一批算法之一。本文會(huì)以圖解的方式詳細(xì)介紹希爾排序的基本思想及其代碼實(shí)現(xiàn)2022-09-09java多線(xiàn)程編程技術(shù)詳解和實(shí)例代碼
這篇文章主要介紹了 java多線(xiàn)程編程技術(shù)詳解和實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04Java數(shù)據(jù)結(jié)構(gòu)順序表的詳細(xì)講解
大家好,今天給大家?guī)?lái)的是順序表,我覺(jué)得順序表還是有比較難理解的地方的,于是我就把這一塊的內(nèi)容全部整理到了一起,希望能夠給剛剛進(jìn)行學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)的人帶來(lái)一些幫助,或者是已經(jīng)學(xué)過(guò)這塊的朋友們帶來(lái)更深的理解,我們現(xiàn)在就開(kāi)始吧2022-05-05spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)維護(hù)的示例代碼
本文主要介紹了spring boot整合flyway實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)維護(hù)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04關(guān)于Maven parent.relativePath說(shuō)明
Maven中的relativePath用于指定父項(xiàng)目pom.xml的相對(duì)路徑,默認(rèn)值為../pom.xml,這個(gè)配置幫助Maven在構(gòu)建時(shí)定位父模塊的位置,確保模塊間的依賴(lài)關(guān)系正確,relativePath可以指向本地或遠(yuǎn)程倉(cāng)庫(kù)中的父項(xiàng)目,如果不需要尋找父項(xiàng)目,可以將其設(shè)置為空2024-09-09