我面临着让Epoch在当天开始和结束时排名第二的问题。 即8月12日00:00:00和8月23日9月15日。
答案 0 :(得分:2)
以下两个函数用于返回当天的开始和EPOCH秒的结束。
public static Long getStartOfDayEpochSecond() {
long secondInaDay = 60 * 60 * 24;
long currentMilliSecond = System.currentTimeMillis() / 1000;
long startOfTheDayEpoch= currentMilliSecond - (currentMilliSecond %secondInaDay);
return startOfTheDayEpoch;
}
//将endOfTheDayEpoch减去-1,因为我们在当天结束时使用23.59.59。
public static Long getEndOfDayEpochSecond() {
long startOfTheDayEpoch=getStartOfDayEpochSecond();
long secondInaDay = 60 * 60 * 24;
long endOfTheDayEpoch = dateFrom + secondInaDay-1;
return endOfTheDayEpoch;
}
答案 1 :(得分:2)
希望这会有所帮助:
public long getStartOfDayInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public long getEndOfDayInMillis() {
// Add one day's time to the beginning of the day.
// 24 hours * 60 minutes * 60 seconds * 1000 milliseconds = 1 day
return getStartOfDayInMillis() + (24 * 60 * 60 * 1000);
}