我已经在这个代码上工作了 2 天,我所拥有的就是这个。我想我取得了一些进展,我想我差不多完成了。我唯一的问题是在我的公共 Time(long elapseTime) 构造函数中。读出的格式不正确或我做错了什么。Time t2 = new Time(1382832000) 应该显示 384,120 小时,但它只显示 384 小时。我究竟做错了什么?
public class Time{
private long hour;
private long minute;
private long second;
public Time(){
//this(System.currentTimeMillis());
second = (int) (System.currentTimeMillis()/ 1000) % 60 ;
minute = (int) (System.currentTimeMillis() / 1000 / 60) % 60;
hour = (int) (System.currentTimeMillis() / 1000 / 60 / 60) % 24;
}
public Time(long elapseTime){
//setTime(elapseTime);
second = (elapseTime / 1000);
minute = (elapseTime / 1000 / 60);
hour = (elapseTime / 1000 / 60 / 60);
}
Time(int hour, int minute, int second){
/*hour = ((this.hour >= 0 && this.hour < 24) ? this.hour : 0);
minute = ((this.minute >= 0 && this.minute < 6) ? this.minute : 0);
second = ((this.second >= 0 && this.second < 24) ? this.second : 0);*/
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void setTime(long elapseTime){
second = (int) (elapseTime / 1000) % 60 ;
minute = (int) ((elapseTime / (1000*60)) % 60);
hour = (int) ((elapseTime / (1000*60*60)) % 24);
}
public long getHour() {
return hour;
}
public long getMinute() {
return minute;
}
public long getSecond() {
return second;
}
public String toString(){
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
}
测试它...
public class TestTimeClass{
public static void main(String [] args){
Time t1 = new Time();
System.out.println(t1);
//System.out.println(System.currentTimeMillis());
Time t2 = new Time(1382832000);
System.out.println(t2);
t2.setTime(555550000);
System.out.println(t2);
}
}
输出为 ... 22:50:47
384:23047:1382832 // 这应该更像 384,120 : 23,047,200 : 1,382,832,000
10:19:10