1. 程式人生 > 其它 >DataUtils 時間工具類

DataUtils 時間工具類

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();
}



}