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

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

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

前言

不論是什么編程語言,無論是剛開始學習,還是做了多長時間的猿,都離不開時間戳和時間的轉(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)默認時區(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中獲取時間戳以及通過實踐對象獲取時間戳和通過時間戳獲取時間對象。

總結(jié)

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

相關(guān)文章

  • 自定義feignClient的常見坑及解決

    自定義feignClient的常見坑及解決

    這篇文章主要介紹了自定義feignClient的常見坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Gson如何序列化內(nèi)部類

    Gson如何序列化內(nèi)部類

    本文介紹如何使用Gson對成員內(nèi)部類及靜態(tài)內(nèi)部類進行序列化/反序列化,幫助大家更好的理解和使用gson庫,感興趣的朋友可以了解下
    2020-11-11
  • 解決IDEA JSP沒有代碼提示問題的幾種方法

    解決IDEA JSP沒有代碼提示問題的幾種方法

    這篇文章主要介紹了解決IDEA JSP沒有代碼提示問題的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • MyBatisPlus超詳細分析條件查詢

    MyBatisPlus超詳細分析條件查詢

    這篇文章主要介紹了MyBatisPlus條件查詢的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • SpringBoot自定義maven-plugin插件整合asm代碼插樁

    SpringBoot自定義maven-plugin插件整合asm代碼插樁

    本文主要介紹了SpringBoot自定義maven-plugin插件整合asm代碼插樁,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • mybatis中一對一、一對多的<association> 配置使用

    mybatis中一對一、一對多的<association> 配置使用

    <association>?是 MyBatis 中處理一對一或一對多關(guān)系的映射元素,用于處理一對一和一對多關(guān)系的映射,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-05-05
  • Java中使用Spring Retry實現(xiàn)重試機制的流程步驟

    Java中使用Spring Retry實現(xiàn)重試機制的流程步驟

    這篇文章主要介紹了我們將探討如何在Java中使用Spring Retry來實現(xiàn)重試機制,重試機制在處理臨時性故障和提高系統(tǒng)穩(wěn)定性方面非常有用,文中通過代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-07-07
  • java判斷遠程服務(wù)器上的文件是否存在的方法

    java判斷遠程服務(wù)器上的文件是否存在的方法

    java判斷遠程服務(wù)器上的文件是否存在的方法,需要的朋友可以參考一下
    2013-03-03
  • SpringBoot項目實現(xiàn)分布式日志鏈路追蹤

    SpringBoot項目實現(xiàn)分布式日志鏈路追蹤

    這篇文章主要給大家介紹了Spring Boot項目如何實現(xiàn)分布式日志鏈路追蹤,文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • Java實現(xiàn)兩人五子棋游戲(六) 行棋方變換

    Java實現(xiàn)兩人五子棋游戲(六) 行棋方變換

    這篇文章主要為大家詳細介紹了Java實現(xiàn)一個簡單的兩人五子棋游戲,行棋方變換,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評論

通州区| 和龙市| 新巴尔虎左旗| 永兴县| 班戈县| 山东省| 滦平县| 平遥县| 青州市| 屏东县| 阿城市| 吴堡县| 荥经县| 句容市| 庄浪县| 房产| 庄浪县| 上栗县| 如皋市| 清原| 陇川县| 惠水县| 睢宁县| 琼海市| 平陆县| 金寨县| 新田县| 宣汉县| 竹北市| 厦门市| 怀远县| 湖口县| 泰来县| 新沂市| 嫩江县| 济阳县| 开化县| 石家庄市| 潍坊市| 靖宇县| 铜山县|