Java中時(shí)間戳和時(shí)間的轉(zhuǎn)換方法代碼
前言
不論是什么編程語(yǔ)言,無(wú)論是剛開始學(xué)習(xí),還是做了多長(zhǎng)時(shí)間的猿,都離不開時(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
//獲取我們常見的時(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)文章
解決IDEA JSP沒(méi)有代碼提示問(wèn)題的幾種方法
這篇文章主要介紹了解決IDEA JSP沒(méi)有代碼提示問(wèn)題的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
SpringBoot自定義maven-plugin插件整合asm代碼插樁
本文主要介紹了SpringBoot自定義maven-plugin插件整合asm代碼插樁,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
mybatis中一對(duì)一、一對(duì)多的<association> 配置使用
<association>?是 MyBatis 中處理一對(duì)一或一對(duì)多關(guān)系的映射元素,用于處理一對(duì)一和一對(duì)多關(guān)系的映射,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
Java中使用Spring Retry實(shí)現(xiàn)重試機(jī)制的流程步驟
這篇文章主要介紹了我們將探討如何在Java中使用Spring Retry來(lái)實(shí)現(xiàn)重試機(jī)制,重試機(jī)制在處理臨時(shí)性故障和提高系統(tǒng)穩(wěn)定性方面非常有用,文中通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-07-07
java判斷遠(yuǎn)程服務(wù)器上的文件是否存在的方法
java判斷遠(yuǎn)程服務(wù)器上的文件是否存在的方法,需要的朋友可以參考一下2013-03-03
SpringBoot項(xiàng)目實(shí)現(xiàn)分布式日志鏈路追蹤
這篇文章主要給大家介紹了Spring Boot項(xiàng)目如何實(shí)現(xiàn)分布式日志鏈路追蹤,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
Java實(shí)現(xiàn)兩人五子棋游戲(六) 行棋方變換
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的兩人五子棋游戲,行棋方變換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

