java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java获取时间和时间戳

Java如何获取当天零点和明天零点的时间和时间戳

作者:宁宁可可

这篇文章主要介绍了如何在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获取时间和时间戳内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文