I have been working on this code for 2 days and all I have is this. I think I made some progress and I think I'm almost done. The only problem I have is in my public Time(long elapseTime) constructor. The read out is not formatted correctly or I did something wrong. Time t2 = new Time(1382832000) should display 384,120 hours but it only displays 384 hours. What am I doing wrong?
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());
}
}
to test it...
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);
}
}
the output is ...
22:50:47
384:23047:1382832 // this should be more like 384,120 : 23,047,200 : 1,382,832,000
10:19:10
Your class is very confusing. You seem to want to represent both 24 hour time of day as well as elapsed time.
Also, you should check your math since 1382832000 milliseconds is 384 hours.