public class DataUtils {
/**
*
* @description: 获得当天最小时间
* @author: Jeff
* @date: 2019年12月21日
* @param date
* @return
*/
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());
}
/**
*
* @description: 获得当天最大时间
* @author: Jeff
* @date: 2019年12月21日
* @param date
* @return
*/
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());
}
/*
* 获取当前时间多少小时前的时差
* */
public static Date getSubHours(Calendar c, int amount) {
Date courrentTime = c.getTime();
c.setTime(new Date());
c.add(Calendar.HOUR, -amount);
return c.getTime();
}
/*
* 获取当前时间多少天
* */
public static Date getSubDay(Calendar c, int amount) {
Date courrentTime = c.getTime();
c.setTime(new Date());
c.add(Calendar.DATE, -amount);
return c.getTime();
}
/**
* 获取当时时间N月前的时间
* @param time
* @param month
* @return
*/
public static Date getBeforeMonthTime(Date time,int month) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -month);
Date date = c.getTime();
return date;
}
/**
* 获取当时时间N月后的时间
* @param time
* @param month
* @return
*/
public static Date getAfterMonthTime(Date time,int month) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, month);
Date date = c.getTime();
return date;
}
}