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)文章
SpringBoot整合Spring Data Elasticsearch的過(guò)程詳解
這篇文章主要介紹了SpringBoot整合Spring Data Elasticsearch的過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java實(shí)現(xiàn)文件上傳下載和圖片壓縮代碼示例
本文給大家介紹的是項(xiàng)目中經(jīng)常需要用到的一個(gè)常用的功能,使用java實(shí)現(xiàn)文件的上傳下載和圖片的壓縮功能,這里推薦給大家,有需要的小伙伴參考下。2015-03-03
基于strict-origin-when-cross-origin問(wèn)題的解決
這篇文章主要介紹了基于strict-origin-when-cross-origin問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring Cloud 配置中心內(nèi)容加密的配置方法
這篇文章主要介紹了Spring Cloud 配置中心內(nèi)容加密的配置方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
SpringBoot處理HTTP請(qǐng)求的詳細(xì)流程
這篇文章主要介紹了SpringBoot處理HTTP請(qǐng)求的詳細(xì)流程,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05
Spring?Security實(shí)現(xiàn)基于RBAC的權(quán)限表達(dá)式動(dòng)態(tài)訪問(wèn)控制的操作方法
這篇文章主要介紹了Spring?Security實(shí)現(xiàn)基于RBAC的權(quán)限表達(dá)式動(dòng)態(tài)訪問(wèn)控制,資源權(quán)限表達(dá)式動(dòng)態(tài)權(quán)限控制在Spring Security也是可以實(shí)現(xiàn)的,首先開(kāi)啟方法級(jí)別的注解安全控制,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
SpringBoot中利用MyBatis進(jìn)行數(shù)據(jù)操作的示例
這篇文章主要介紹了SpringBoot中利用MyBatis進(jìn)行數(shù)據(jù)操作,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
java使用lambda表達(dá)式對(duì)List集合進(jìn)行操作技巧(JDK1.8)
這篇文章主要介紹了java使用lambda表達(dá)式對(duì)List集合進(jìn)行操作技巧適用jdk1.8,感興趣的朋友跟著小編一起看看實(shí)現(xiàn)代碼吧2018-06-06

