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);}}
执行结果: