日期处理,获取任意时间前后几天时间,当天最大、最小时间
获取几天前日期为-n,几天后日期则为+n
/**
* @description: 获取几天前(后)的日期
* @param dayTime
* @param addDayTime
* @return: java.util.Date
* @author: Alm
* @time: 2021/5/26 15:18
*/
public static Date getDay(int dayTime,int addDayTime){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(dayTime,addDayTime);
Date date = calendar.getTime();
return date;
}
/**
* @description: 获取指定日期几天前(后)的日期
* @param date
* @param dayTime
* @param addDayTime
* @return: java.util.Date
* @author: Alm
* @time: 2021/5/27 10:36
*/
public static Date getDayByDay(Date date,int dayTime,int addDayTime){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(dayTime,addDayTime);
Date resultDate = calendar.getTime();
return resultDate;
}
/**
* @description: 获取当日最大时间
* @param date
* @return: java.util.Date
* @author: Alm
* @time: 2021/5/26 11:07
*/
public static Date getEndOfDay(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* @description: 获取当日最小时间
* @param date
* @return: java.util.Date
* @author: Alm
* @time: 2021/5/26 11:08
*/
public static Date getStartOfDay(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
}
具体调用
Date date = TimesUtil.getDay(Calendar.DATE,-1);
Date endDate = TimesUtil.getEndOfDay(date);
Date startDate = TimesUtil.getStartOfDay(date);