Java 获取前一天的24小时
//获取凌晨时间
public static Date getTodayStartTime(){
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, 0);
todayEnd.set(Calendar.MINUTE, 5);
todayEnd.set(Calendar.SECOND, 0);
todayEnd.set(Calendar.MILLISECOND, 0);
return todayEnd.getTime();
}
//获取昨天半夜23:59:59
public static Date getYesterdayEnd(){
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 0);
todayEnd.add(Calendar.DATE, -1);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
return todayEnd.getTime();
}
//获取某时刻过去的24小时
public static Date getOneDayBefore(Date dateEnd){
Calendar date = Calendar.getInstance();
date.setTime(dateEnd);
date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
return date.getTime();
}
强迫症,反对String字符串转来转去。