本文介绍了在java中设计一个名为Time的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个代码工作2天,我所有的是这个。我想我取得了一些进展,我想我差不多完成了。我唯一的问题是在我的公共时间(long elapseTime)构造函数。读出的格式不正确,或者我做错了。时间t2 =新时间(1382832000)应显示384,120小时,但它只显示384小时。

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());
   }

}

/ p>

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);

   }

}

...
22:50:47

384:23047:1382832 //这应该更像384,120:23,047,200:1,382,832,000

10:19:10


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

推荐答案

你的班很混乱。

此外,你应该检查你的数学,因为1382832000毫秒是384小时。

Also, you should check your math since 1382832000 milliseconds is 384 hours.

这篇关于在java中设计一个名为Time的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:54