首先说一下:通过加减时间的long型毫秒值来获取0点的时间戳,是不可靠的,有可能获取的是前一天0点的时间戳。有问题的算法比如这样:
//获取当天(按当前传入的时区)00:00:00所对应时刻的long型值
private long getStartTimeOfDay(long now, String timeZone) {
String tz = TextUtils.isEmpty(timeZone) ? "GMT+8" : timeZone;
TimeZone curTimeZone = TimeZone.getTimeZone(tz);
long oneDay = TimeUnit.DAYS.toMillis(1);//一天时间的毫秒值
return (now - (now % oneDay) - curTimeZone.getRawOffset());
}
正确的是通过Canlendar类去获取,比如:
//获取当天(按当前传入的时区)00:00:00所对应时刻的long型值
private long getStartTimeOfDay(long now, String timeZone) {
String tz = TextUtils.isEmpty(timeZone) ? "GMT+8" : timeZone;
TimeZone curTimeZone = TimeZone.getTimeZone(tz);
Calendar calendar = Calendar.getInstance(curTimeZone);
calendar.setTimeInMillis(now);
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();
}