最新国产好看的视频,伊人天堂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)論

临汾市| 桐梓县| 阿尔山市| 荥经县| 克什克腾旗| 专栏| 眉山市| 寿宁县| 绥阳县| 大宁县| 信丰县| 米易县| 安国市| 赣榆县| 乐安县| 瑞丽市| 普格县| 阿克陶县| 临沧市| 改则县| 易门县| 宜宾县| 南郑县| 瑞安市| 高阳县| 肥西县| 北京市| 达州市| 桦南县| 合作市| 阆中市| 苏尼特右旗| 墨脱县| 石渠县| 阿拉善右旗| 西乌珠穆沁旗| 马山县| 交口县| 九龙城区| 新巴尔虎左旗| 定结县|