java用LocalDateTime類獲取當(dāng)天時(shí)間、前一天時(shí)間及本周/本月的開始和結(jié)束時(shí)間
1.直接上代碼:
// 獲取當(dāng)天日期 LocalDate now = LocalDate.now(); // 當(dāng)天開始時(shí)間 LocalDateTime todayStart = now.atStartOfDay(); // 當(dāng)天結(jié)束時(shí)間 LocalDateTime todayEnd = LocalDateTime.of(now, LocalTime.MAX); // 周一 LocalDate monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // 周日 LocalDate sunday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // 本周開始時(shí)間 LocalDateTime weekStart = monday.atStartOfDay(); // 本周結(jié)束時(shí)間 LocalDateTime weekEnd = LocalDateTime.of(sunday, LocalTime.MAX); // 本月1號(hào) LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth()); // 本月最后一天 LocalDate lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()); // 本月1號(hào)的開始時(shí)間 LocalDateTime firstDayOfMonthStart = firstDayOfMonth.atStartOfDay(); // 本月最后一天的最后時(shí)間 LocalDateTime firstDayOfMonthEnd = LocalDateTime.of(lastDayOfMonth, LocalTime.MAX); // 今年第一天 LocalDate beginTime = LocalDate.now().with(TemporalAdjusters.firstDayOfYear()); // 今年最后一天 LocalDate endTiime = LocalDate.now().with(TemporalAdjusters.lastDayOfYear()); //獲取前一天日期 LocalDate yesterday2 = LocalDate.now().minusDays(1); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println("當(dāng)天開始時(shí)間 = " + todayStart.format(pattern) + " 時(shí)間戳 :" + todayStart.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("當(dāng)天結(jié)束時(shí)間 = " + todayEnd.format(pattern) + " 時(shí)間戳 :" + todayEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本周開始時(shí)間 = " + weekStart.format(pattern) + " 時(shí)間戳 :" + weekStart.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本周結(jié)束時(shí)間 = " + weekEnd.format(pattern) + " 時(shí)間戳 :" + weekEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本月開始時(shí)間 = " + firstDayOfMonthStart.format(pattern) + " 時(shí)間戳 :" + firstDayOfMonthStart.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本月結(jié)束時(shí)間 = " + firstDayOfMonthEnd.format(pattern) + " 時(shí)間戳 :" + firstDayOfMonthEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli());
2.當(dāng)前時(shí)間減
LocalDate.now().minusYears(x); //當(dāng)前日期減去指定的年份 ???????LocalDate.now().minusMonths(x); //當(dāng)前日期減去指定的月份 LocalDate.now().minusDays(x); //當(dāng)前日期減去指定的天數(shù) LocalDate.now().minusWeeks(x); //當(dāng)前日期減去指定的周數(shù)
3.時(shí)間格式化
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"); //格式化 String str4 = formatter3.format(LocalDateTime.now()); System.out.println(str4);//2021-09-22 06:33:08 //解析 TemporalAccessor parse1 = formatter3.parse(str4); System.out.println(parse1);//{NanoOfSecond=0, MilliOfSecond=0, MinuteOfHour=34, HourOfAmPm=6, SecondOfMinute=6, MicroOfSecond=0},ISO resolved to 2021-09-22 //解析 String dateString = "2023-07-13 15:30:00"; LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter3);
注意:
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
這個(gè) hh:mm:ss 跟HH:mm:ss的區(qū)別,如果采用24小時(shí)的話,要用HH,如果是:2023-07-27 23:59:59 這種,確用了 hh:mm:ss 這種格式,會(huì)報(bào)錯(cuò)
4.兩個(gè)LocalDateTime.now()之間相差的天數(shù)
LocalDateTime start = LocalDateTime.of(2023, 6, 1, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2023, 6, 28, 0, 0, 0); Duration duration = Duration.between(start, end); long days = duration.toDays(); System.out.println(days); // 輸出:27
總結(jié)
到此這篇關(guān)于java用LocalDateTime類獲取當(dāng)天時(shí)間、前一天時(shí)間及本周/本月的開始和結(jié)束時(shí)間的文章就介紹到這了,更多相關(guān)java LocalDateTime類獲取時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談java+內(nèi)存分配及變量存儲(chǔ)位置的區(qū)別
下面小編就為大家?guī)硪黄獪\談java+內(nèi)存分配及變量存儲(chǔ)位置的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08Springboot webscoket自定義定時(shí)器
這篇文章主要介紹了Springboot webscoket自定義定時(shí)器問題,總的來說這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過程。希望通過這道題能給你帶來一種解題優(yōu)化的思路2023-01-01java 動(dòng)態(tài)代理的方法總結(jié)
這篇文章主要介紹了java 動(dòng)態(tài)代理的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04基于SpringBoot和Vue3的博客平臺(tái)文章列表與分頁功能實(shí)現(xiàn)
在前面的教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能。本教程將繼續(xù)引導(dǎo)您實(shí)現(xiàn)博客平臺(tái)的文章列表與分頁功能,需要的朋友可以參考閱讀2023-04-04Java Swing JSlider滑塊的實(shí)現(xiàn)示例
這篇文章主要介紹了Java Swing JSlider滑塊的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java getParameter()獲取數(shù)據(jù)為空的問題
這篇文章主要介紹了Java getParameter()獲取數(shù)據(jù)為空的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03