Java如何獲取當(dāng)天零點和明天零點的時間和時間戳
Java獲取當(dāng)天零點和明天零點的時間和時間戳
//1.獲取當(dāng)前時間 LocalDate today = LocalDate.now(); //2.轉(zhuǎn)換成LocalDateTime對象 LocalDateTime todayMidnight = LocalDateTime.of(today, LocalTime.MIN); System.out.println("當(dāng)天0點0分0秒的時間:"+todayMidnight); //3.將LocalDateTime對象轉(zhuǎn)換成時間戳 long startTime = todayMidnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("當(dāng)天0點0分0秒的時間戳:"+startTime); //1.獲取當(dāng)前日期+1天 LocalDate tomorrow = today.plusDays(1); //2.獲取明天的時間 LocalDateTime tomorrowMidnight=tomorrow.atStartOfDay(); System.out.println("第二天0點0分0秒時間:"+tomorrowMidnight); //3.將LocalDateTime對象轉(zhuǎn)換成時間戳 long endTime = tomorrowMidnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("第二天0點0分0秒的時間戳:"+endTime);
package animals; import java.time.*; /** * Description : * * @author : HMF * Date : Created in 15:31 2024/11/7 * @version : */ public class test003 { public static void main(String[] args){ //1.獲取當(dāng)前時間 LocalDate today = LocalDate.now(); //2.轉(zhuǎn)換成LocalDateTime對象 LocalDateTime todayMidnight = LocalDateTime.of(today, LocalTime.MIN); System.out.println("當(dāng)天0點0分0秒的時間:"+todayMidnight); //3.將LocalDateTime對象轉(zhuǎn)換成時間戳 long startTime = todayMidnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("當(dāng)天0點0分0秒的時間戳:"+startTime); //1.獲取當(dāng)前日期+1天 LocalDate tomorrow = today.plusDays(1); //2.獲取明天的時間 LocalDateTime tomorrowMidnight=tomorrow.atStartOfDay(); System.out.println("第二天0點0分0秒時間:"+tomorrowMidnight); //3.將LocalDateTime對象轉(zhuǎn)換成時間戳 long endTime = tomorrowMidnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("第二天0點0分0秒的時間戳:"+endTime); } }
執(zhí)行結(jié)果:
總結(jié)
到此這篇關(guān)于Java如何獲取當(dāng)天零點和明天零點的時間和時間戳的文章就介紹到這了,更多相關(guān)Java獲取時間和時間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)通過IP獲取IP歸屬地的方法(離線+在線)
我們都知道安全攻擊都是在某臺客戶機上執(zhí)行某些惡意操作致使服務(wù)端響應(yīng)異常崩潰亦或響應(yīng)數(shù)據(jù)被篡改,首先我想到的是對訪問的web端做一個IP的校驗,那么我們首先得知道客戶端的IP是多少,接下來此文重點介紹如何獲得,需要的朋友可以參考下2023-10-10解決springboot的JPA在Mysql8新增記錄失敗的問題
這篇文章主要介紹了解決springboot的JPA在Mysql8新增記錄失敗的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06使用監(jiān)聽器對Spring bean id進行唯一校驗過程解析
這篇文章主要介紹了使用監(jiān)聽器對Spring bean id進行唯一校驗過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08基于SpringBoot解決CORS跨域的問題(@CrossOrigin)
這篇文章主要介紹了基于SpringBoot解決CORS跨域的問題(@CrossOrigin),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Spring Boot實現(xiàn)數(shù)據(jù)訪問計數(shù)器方案詳解
在Spring Boot項目中,有時需要數(shù)據(jù)訪問計數(shù)器,怎么實現(xiàn)數(shù)據(jù)訪問計數(shù)器呢?下面小編給大家?guī)砹薙pring Boot數(shù)據(jù)訪問計數(shù)器的實現(xiàn)方案,需要的朋友參考下吧2021-08-08Java純代碼實現(xiàn)導(dǎo)出pdf合并單元格
這篇文章主要為大家詳細介紹了Java如何純代碼實現(xiàn)導(dǎo)出pdf與合并單元格功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12Java 程序里transient關(guān)鍵字使用方法示例
這篇文章主要為大家介紹了Java 程序里transient關(guān)鍵字使用方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11