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获取时间和时间戳内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决springboot的JPA在Mysql8新增记录失败的问题
这篇文章主要介绍了解决springboot的JPA在Mysql8新增记录失败的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06
使用监听器对Spring bean id进行唯一校验过程解析
这篇文章主要介绍了使用监听器对Spring bean id进行唯一校验过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-08-08
基于SpringBoot解决CORS跨域的问题(@CrossOrigin)
这篇文章主要介绍了基于SpringBoot解决CORS跨域的问题(@CrossOrigin),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-01-01


最新评论