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

JAVA中時(shí)間戳與LocalDateTime互相轉(zhuǎn)換代碼例子

 更新時(shí)間:2023年11月20日 11:10:16   作者:海邊的漫徹斯特  
最近在編碼過程中遇到將時(shí)間戳轉(zhuǎn)化為 LocalDateTime,所以這里給總結(jié)下,這篇文章主要給大家介紹了關(guān)于JAVA中時(shí)間戳與LocalDateTime互相轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下

時(shí)間戳轉(zhuǎn)LocalDateTime:

要將時(shí)間戳轉(zhuǎn)換為L(zhǎng)ocalDateTime并將LocalDateTime轉(zhuǎn)換回時(shí)間戳,使用Java的java.time包。以下是示例代碼:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class TimestampToLocalDateTime {
    public static void main(String[] args) {
    	// 注意:這里是秒級(jí)時(shí)間戳
        long timestamp = 1692948472; 

        // 使用Instant從時(shí)間戳創(chuàng)建時(shí)間點(diǎn)
        Instant instant = Instant.ofEpochSecond(timestamp);

        // 使用ZoneId定義時(shí)區(qū)(可以根據(jù)需要選擇不同的時(shí)區(qū))
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");

        // 將Instant轉(zhuǎn)換為L(zhǎng)ocalDateTime
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

        System.out.println("時(shí)間戳: " + timestamp);
        System.out.println("轉(zhuǎn)換后的LocalDateTime: " + localDateTime);
    }
}

LocalDateTime轉(zhuǎn)時(shí)間戳:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class LocalDateTimeToTimestamp {
    public static void main(String[] args) {
        // 創(chuàng)建一個(gè)LocalDateTime對(duì)象
        LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 25, 0, 0);

        // 使用ZoneId定義時(shí)區(qū)(可以根據(jù)需要選擇不同的時(shí)區(qū))
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");

        // 將LocalDateTime轉(zhuǎn)換為Instant
        Instant instant = localDateTime.atZone(zoneId).toInstant();

        // 獲取時(shí)間戳
        long timestamp = instant.getEpochSecond();

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("轉(zhuǎn)換后的時(shí)間戳: " + timestamp);
    }
}

附:LocalDate,LocalDateTime,Date及時(shí)間戳的轉(zhuǎn)換

1.時(shí)間戳轉(zhuǎn)LocalDateTime,時(shí)間戳如果是字符串則先轉(zhuǎn)為long

LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(System.currentTimeMillis()/1000, 0, ZoneOffset.ofHours(8));

2.日期字符串轉(zhuǎn)LocalDate

LocalDate parse = LocalDate.parse(“2020-05-13”);

3.LocalDateTime轉(zhuǎn)LocalDate

LocalDate localDate = LocalDateTime.now().toLocalDate();

4.Date轉(zhuǎn)LocalDate

Date date = new Date();
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();

或使用系統(tǒng)默認(rèn)時(shí)區(qū)

LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

5.時(shí)間戳轉(zhuǎn)Date

Date date = new Date(System.currentTimeMillis());

date.setTime(System.currentTimeMillis());

6.Date轉(zhuǎn)LocalDateTime

LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());

7.LocalDateTime轉(zhuǎn)Date

Date date1 = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());

8.LocalDate轉(zhuǎn)Date

ZonedDateTime zonedDateTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault());
Date date = Date.from(zonedDateTime.toInstant());

總結(jié) 

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

相關(guān)文章

最新評(píng)論

深圳市| 海城市| 郴州市| 奉新县| 平阴县| 兴宁市| 离岛区| 凤翔县| 互助| 玉田县| 咸丰县| 兖州市| 吴堡县| 左贡县| 扎赉特旗| 从江县| 佛坪县| 南溪县| 惠东县| 温宿县| 潼南县| 正定县| 云和县| 沂源县| 吉安县| 沾化县| 将乐县| 剑河县| 阿城市| 噶尔县| 济宁市| 台安县| 靖边县| 鄢陵县| 镇安县| 衡南县| 集贤县| 绥江县| 荆门市| 榕江县| 和田县|