Java如何获取当天零点和明天零点的时间和时间戳
更新时间:2025年03月12日 10:34:11 作者:宁宁可可
这篇文章主要介绍了如何在Java中获取当天零点和明天零点的时间和时间戳,并提供了示例代码,新手小白完全可以通过文中介绍的代码实现,需要的朋友可以参考下
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);
}
}
执行结果:

总结
到此这篇关于Java如何获取当天零点和明天零点的时间和时间戳的文章就介绍到这了,更多相关Java获取时间和时间戳内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Boot中使用RabbitMQ 生产消息和消费消息的实例代码
本文介绍了在SpringBoot中如何使用RabbitMQ进行消息的生产和消费,详细阐述了RabbitMQ中交换机的作用和类型,包括直连交换机、主题交换机、扇出交换机和头交换机,并解释了各自的消息路由机制,感兴趣的朋友一起看看吧2024-10-10
Spring Boot + Mybatis多数据源和动态数据源配置方法
最近做项目遇到这样的应用场景,项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库。下面小编给大家带来了Spring Boot + Mybatis多数据源和动态数据源配置方法,需要的朋友参考下吧2018-01-01
Spring Cloud 部署时使用 Kubernetes 作为注册中心和配置中
Spring Cloud Kubernetes提供了使用Kubernete本地服务的Spring Cloud通用接口实现,这篇文章主要介绍了Spring Cloud 部署时如何使用 Kubernetes 作为注册中心和配置中心,需要的朋友可以参考下2024-05-05
Java String源码分析并介绍Sting 为什么不可变
这篇文章主要介绍了Java String源码分析并介绍Sting 为什么不可变的相关资料,需要的朋友可以参考下2017-02-02
Spring监听器之ApplicationListener原理及源码深度解析
本文介绍了Spring事件机制的原理理及源码解析,重点了ContextRefreshedEvent和ContextClosededEvent事件;并详细描了事件发布流程、事件多播器器的源码执行流程;以及容器中监听器的管理;最后举了一个自定义监听器的例子以然示如何利用事件机制,感兴趣的朋友一起看看吧2026-04-04


最新评论