1. 程式人生 > >實現時間 秒加,分鐘加,小時加的Timer類

實現時間 秒加,分鐘加,小時加的Timer類

                                                                實現時間 秒加,分鐘加,小時加的Timer類


package abc;
@SuppressWarnings("all")
public class Timer {



    private int seconds = 0;  //定義秒
    private int minutes = 0;  //定義分
    private int hours = 0; //定義時

    public int getSeconds() {
return seconds;
}

public void setSeconds(int seconds) {  
this.seconds = seconds;
if(this.seconds<60){
this.seconds = this.seconds; 


}else if(this.seconds>=60&&this.seconds%60==0){  
this.minutes = this.minutes + this.seconds/60;
this.seconds = 0;

}else if(this.seconds>=60&&this.seconds%60!=0){
this.minutes = this.minutes + this.seconds/60;
this.seconds = this.seconds%60;
}
}

public int getMinutes() {
return minutes;
}

public void setMinutes(int minutes) {


this.minutes = this.minutes + minutes;
if(this.minutes<60){
this.minutes = this.minutes;
}else if(this.minutes>=60&&this.minutes%60==0){
this.hours = this.hours + this.minutes/60;
this.minutes = 0;
}else if(this.minutes>=60&&this.minutes%60!=0){
this.hours = this.hours + this.minutes/60;
this.minutes = this.minutes%60;
}
}

public int getHours() {
return hours;
}

public void setHours(int hours) {
this.hours = this.hours + hours;
}

public static void main(String[] args) {
Timer tt = new Timer();

                 /*測試時、分、秒*/
                 tt.setSeconds(308);  

                 tt.setMinutes(77);
                 tt.setHours(34);  
                 System.out.println(tt.getHours() + "時" + tt.getMinutes() + "分" + tt.getSeconds() + "秒");
}
}