Java如何獲取當天零點和明天零點的時間和時間戳
更新時間:2025年03月12日 10:34:11 作者:寧寧可可
這篇文章主要介紹了如何在Java中獲取當天零點和明天零點的時間和時間戳,并提供了示例代碼,新手小白完全可以通過文中介紹的代碼實現(xiàn),需要的朋友可以參考下
Java獲取當天零點和明天零點的時間和時間戳
//1.獲取當前時間
LocalDate today = LocalDate.now();
//2.轉換成LocalDateTime對象
LocalDateTime todayMidnight = LocalDateTime.of(today, LocalTime.MIN);
System.out.println("當天0點0分0秒的時間:"+todayMidnight);
//3.將LocalDateTime對象轉換成時間戳
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對象轉換成時間戳
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.轉換成LocalDateTime對象
LocalDateTime todayMidnight = LocalDateTime.of(today, LocalTime.MIN);
System.out.println("當天0點0分0秒的時間:"+todayMidnight);
//3.將LocalDateTime對象轉換成時間戳
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對象轉換成時間戳
long endTime = tomorrowMidnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println("第二天0點0分0秒的時間戳:"+endTime);
}
}
執(zhí)行結果:

總結
到此這篇關于Java如何獲取當天零點和明天零點的時間和時間戳的文章就介紹到這了,更多相關Java獲取時間和時間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot中使用RabbitMQ 生產(chǎn)消息和消費消息的實例代碼
本文介紹了在SpringBoot中如何使用RabbitMQ進行消息的生產(chǎn)和消費,詳細闡述了RabbitMQ中交換機的作用和類型,包括直連交換機、主題交換機、扇出交換機和頭交換機,并解釋了各自的消息路由機制,感興趣的朋友一起看看吧2024-10-10
Spring Boot + Mybatis多數(shù)據(jù)源和動態(tài)數(shù)據(jù)源配置方法
最近做項目遇到這樣的應用場景,項目需要同時連接兩個不同的數(shù)據(jù)庫A, B,并且它們都為主從架構,一臺寫庫,多臺讀庫。下面小編給大家?guī)砹薙pring Boot + Mybatis多數(shù)據(jù)源和動態(tài)數(shù)據(jù)源配置方法,需要的朋友參考下吧2018-01-01
Spring Cloud 部署時使用 Kubernetes 作為注冊中心和配置中
Spring Cloud Kubernetes提供了使用Kubernete本地服務的Spring Cloud通用接口實現(xiàn),這篇文章主要介紹了Spring Cloud 部署時如何使用 Kubernetes 作為注冊中心和配置中心,需要的朋友可以參考下2024-05-05
Java String源碼分析并介紹Sting 為什么不可變
這篇文章主要介紹了Java String源碼分析并介紹Sting 為什么不可變的相關資料,需要的朋友可以參考下2017-02-02
Spring監(jiān)聽器之ApplicationListener原理及源碼深度解析
本文介紹了Spring事件機制的原理理及源碼解析,重點了ContextRefreshedEvent和ContextClosededEvent事件;并詳細描了事件發(fā)布流程、事件多播器器的源碼執(zhí)行流程;以及容器中監(jiān)聽器的管理;最后舉了一個自定義監(jiān)聽器的例子以然示如何利用事件機制,感興趣的朋友一起看看吧2026-04-04

