1、通过日历类Calendar
// 获取指定时间的0时 yyyy-MM-dd 00:00:00
public static String getStartDateTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(calendar.getTime());
}
2、通过时间戳方式
// 获取当日时间的0时 yyyy-MM-dd 00:00:00
protected String getTodayStartDateTime() {
long nowTime = System.currentTimeMillis();
long todayStartTime = nowTime - ((nowTime + TimeZone.getDefault().getRawOffset()) % (24 * 60 * 60 * 1000L));
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date(todayStartTime));
}