Java?LocalDateTime常用操作方法
Java8提供了新的時(shí)間接口LocalDateTime。本文主要介紹了Java8中LocalDateTime的一些常用操作方法。不多說(shuō),直接上代碼。欲知詳情,可以看看官網(wǎng)。
LocalDateTime localDateTime = LocalDateTime.now();
//時(shí)間運(yùn)算,相加相減
System.out.println(localDateTime.plusYears(2)); //加2年
System.out.println(localDateTime.plusDays(2)); //加兩天
System.out.println(localDateTime.minusYears(2)); //減兩年
System.out.println(localDateTime.minusDays(2)); //減兩天
System.out.println(localDateTime.toString()); // 轉(zhuǎn)字符串 結(jié)果:2021-11-24T15:36:12.914
System.out.println(localDateTime.toLocalDate()); //獲取日期(LocalDate) 結(jié)果:2021-11-24
System.out.println(localDateTime.toLocalTime()); //獲取時(shí)間(LocalTime) 結(jié)果:15:36:12.914
System.out.println(localDateTime.getDayOfMonth()); //獲取當(dāng)前時(shí)間月份的第幾天 結(jié)果:24
System.out.println(localDateTime.getDayOfWeek()); //獲取當(dāng)前周的第幾天 結(jié)果:WEDNESDAY
System.out.println(localDateTime.getDayOfYear()); //獲取當(dāng)前時(shí)間在該年屬于第幾天 結(jié)果:328
System.out.println(localDateTime.getMonthValue()); // 獲取當(dāng)前時(shí)間的月份(阿拉伯文) 結(jié)果:11
System.out.println(localDateTime.getMonth()); // 獲取當(dāng)前時(shí)間的月份(英文) 結(jié)果:11
System.out.println(localDateTime.getHour()); // 獲取當(dāng)前時(shí)間的小時(shí)數(shù) 結(jié)果:15
System.out.println(localDateTime.getMinute()); // 獲取當(dāng)前時(shí)間的分鐘數(shù) 結(jié)果:36
//格式化輸出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
System.out.println(localDateTime.format(formatter)); // 結(jié)果:2021/11/24 15:36:12
//構(gòu)造時(shí)間
LocalDateTime startTime = LocalDateTime.of(2021, 1, 1, 20, 31, 20);
LocalDateTime endTime = LocalDateTime.of(2021, 1, 3, 20, 31, 20);
//比較時(shí)間
System.out.println(localDateTime.isAfter(startTime)); // 結(jié)果:true
System.out.println(localDateTime.isBefore(endTime)); // 結(jié)果:false
//獲取毫秒數(shù)(使用Instant)
System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); // 結(jié)果:1637739570129
//獲取秒數(shù)(使用Instant)
System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond()); // 結(jié)果:1637739570
// 獲取當(dāng)前時(shí)間的0點(diǎn)~23點(diǎn)
LocalDateTime beginDay = localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endDay = localDateTime.withHour(23).withMinute(59).withSecond(59);
// 獲取本月的第一天的0點(diǎn)0分0秒和最后一天的23點(diǎn)59分59秒
LocalDateTime beginMonth = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
LocalDateTime endMonth = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
// LocalDateTime轉(zhuǎn)Date
Date date = Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
// Date轉(zhuǎn)LocalDateTime
date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
// LocalDateTime獲取秒數(shù)
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
// LocalDateTime獲取毫秒數(shù)
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();效果如下:

v源碼地址
https://github.com/toutouge/javademosecond/tree/master/hellolearn
到此這篇關(guān)于Java LocalDateTime實(shí)用方法的文章就介紹到這了,更多相關(guān)Java LocalDateTime實(shí)用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程CyclicBarrier的實(shí)現(xiàn)代碼
CyclicBarrier可以使一定數(shù)量的線程反復(fù)地在柵欄位置處匯集,本文通過(guò)實(shí)例代碼介紹下Java多線程CyclicBarrier的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-02-02
springmvc接收json串,轉(zhuǎn)換為實(shí)體類List方法
今天小編就為大家分享一篇springmvc接收json串,轉(zhuǎn)換為實(shí)體類List方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
SpringBoot 利用MultipartFile上傳本地圖片生成圖片鏈接的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot 利用MultipartFile上傳本地圖片生成圖片鏈接的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
關(guān)于泛型擦除問(wèn)題的解決--Mybatis查詢類型轉(zhuǎn)換
這篇文章主要介紹了關(guān)于泛型擦除問(wèn)題的解決--Mybatis查詢類型轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

