展开全部
你的问题是出在了这个方法上currentTimeMillis()
这是这个方法的官方说明32313133353236313431303231363533e4b893e5b19e31333363373835的一部分/*
*
See the description of the class Date
for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
* @see java.util.Date
*/
这个方法拿到的毫秒时间戳是UTC的,也就是国际标准时间,我们中国在东八区,也就是UTC+8时区,是要比UTC时间早8小时的
你代码本身的逻辑没有错,代码输出的小时不对且总比真实时间少了8小时
你在currentTimeMillis()得到时间戳后+上8小时的毫秒数结果就正确了public Time() {
setTime(System.currentTimeMillis()+8*60*60*1000);//+8小时对应的毫秒数
}
//或者
public void setTime(long elapseTime) {
long totalSeconds = elapseTime / 1000;
second = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
minute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
hour = totalHours % 24+8;//这里+8小时
}