Java如何獲取當天零點和明天零點的時間和時間戳
更新時間:2025年03月12日 10:34:11 作者:寧寧可可
這篇文章主要介紹了如何在Java中獲取當天零點和明天零點的時間和時間戳,并提供了示例代碼,新手小白完全可以通過文中介紹的代碼實現(xiàn),需要的朋友可以參考下
Java獲取當天零點和明天零點的時間和時間戳
//1.獲取當前時間 LocalDate today = LocalDate.now(); //2.轉(zhuǎn)換成LocalDateTime對象 LocalDateTime todayMidnight = LocalDateTime.of(today, LocalTime.MIN); System.out.println("當天0點0分0秒的時間:"+todayMidnight); //3.將LocalDateTime對象轉(zhuǎn)換成時間戳 long startTime = todayMidnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("當天0點0分0秒的時間戳:"+startTime); //1.獲取當前日期+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.獲取當前時間 LocalDate today = LocalDate.now(); //2.轉(zhuǎn)換成LocalDateTime對象 LocalDateTime todayMidnight = LocalDateTime.of(today, LocalTime.MIN); System.out.println("當天0點0分0秒的時間:"+todayMidnight); //3.將LocalDateTime對象轉(zhuǎn)換成時間戳 long startTime = todayMidnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("當天0點0分0秒的時間戳:"+startTime); //1.獲取當前日期+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é)
到此這篇關于Java如何獲取當天零點和明天零點的時間和時間戳的文章就介紹到這了,更多相關Java獲取時間和時間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)通過IP獲取IP歸屬地的方法(離線+在線)
我們都知道安全攻擊都是在某臺客戶機上執(zhí)行某些惡意操作致使服務端響應異常崩潰亦或響應數(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進行唯一校驗過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下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-08