Java Time2从3个整数到单个整数

时间:2017-08-17 00:49:05

标签: java

我试图将我的time2类从3个整数更改为单个整数以节省空间。我已经摆脱了小时,分钟和秒,现在只有几秒钟来代表午夜以来的时间。我在set和get方法以及3参数构造函数中替换它,它在Time2Test应用程序中运行,但只读秒。所有三个参数都被读取为秒,所以我不确定发生了什么。 ex- Time2Test时间应为12:25:42,但读取时间为42:42:42。首先是Time2类,然后为了清晰起见我还添加了Time2Test。任何方向都会受到赞赏。

public class Time2 {
    private int totalseconds;
    //no argument constructor
    public Time2()
    {
        this(0,0,0); //invoke constructor with three arguments default to 0
    }

    //constructor with hour supplied minute and second default to 0
    public Time2(int hour)
    {
        this(hour, 0, 0); //invoke constructor with 3 args
    }

    //constructor with hour and minute supplied seconds default to 0
    public Time2(int hour, int minute)
    {
        this(hour, minute, 0); //invoke constructor with 3 args
    }

    //Time2 constructor with hour minute and second supplied also tests

    public Time2(int hour, int minute, int second)
    {       
        this.totalseconds = (hour * 3600);
        this.totalseconds = (minute * 60);
        this.totalseconds = (second);
    }

    public Time2(Time2 time)
    {
        //invoke constructor with 2 args
        this(time.getHour(), time.getMinute(), time.getSecond());
    }

    // SET and GET methods start here, also Universal time conversion and check
    public void setTime(int hour, int minute, int second) 
    {
        if (hour < 0 || hour >= 24)
            throw new IllegalArgumentException("Hour must be 0-23");
        if (minute < 0 || minute >= 59)
            throw new IllegalArgumentException("Minute must be 0-59");
        if (second < 0 || second >= 59)
            throw new IllegalArgumentException("Hour must be 0-59");

         this.totalseconds = (hour * 3600);
         this.totalseconds = (minute * 60);
         this.totalseconds = (second);
    }

    //validate and set hour
    public void setHour(int hour)
    {
        if (hour < 0 || hour >= 24)
            throw new IllegalArgumentException("Hour must be 0-23");
        this.totalseconds = (hour * 3600);
    }

    //validate and set minute
    public void setMinute(int minute)
    {
        if (minute < 0 || minute >= 59)
            throw new IllegalArgumentException("Minute must be 0-59");
        this.totalseconds = (minute * 60);
    }

    //validate and set second
    public void setSecond(int second)
    {
        if (second < 0 || second >= 24)
            throw new IllegalArgumentException("Second must be 0-59");
        this.totalseconds = (second);
    }
    //Get Methods start here

    //Get hour
    public int getHour()
    {
        return totalseconds % 3600;
    }

    //get minute
    public int getMinute()
    {
        return totalseconds % 60;
    }

    //get second
    public int getSecond()
    {
        return totalseconds;
    }

    //convert our string to universal format (HH:MM:SS)
    public String ToUniversalString()
    {
        return String.format(
        "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
    }

    //conver to standard format (H:MM:SS AM or PM)
    public String toString()
    {
        return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() ==
        12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()
        < 12 ? "AM" : "PM"));
    }
}//end class Time2



package time2;

public class Time2Test 
{
    public static void main(String[] args)
    {
        Time2 t1 = new Time2(); //00:00:00
        Time2 t2 = new Time2(2); //02:00:00 
        Time2 t3 = new Time2(21, 34); //21:34:00
        Time2 t4 = new Time2(12, 25, 42); //12:25:42
        Time2 t5 = new Time2(t4); //12:25:42

        System.out.println("Constructed with:");
        displayTime("t1: all default arguments", t1);
        displayTime("t2: hour specified; defaults for minute and second", t2);
        displayTime("t3: hour and minute supplied second defaulted", t3);
        displayTime("t4: hour minute and second supplied", t4);
        displayTime("t5: Time2 object t4 specified", t5);

        //attempt to initialize t6 with invalid args
        try
        {
            Time2 t6 = new Time2(27,74,99); //all invalid values
        }
        catch (IllegalArgumentException e)
        {
            System.out.printf("%nException while initializing t6: %s%n",
                    e.getMessage());
        }
    }
    //display Time2 object in 24 hour and 12 hour formats
    private static void displayTime(String header, Time2 t)
    {
        System.out.printf("%s%n   %s%n   %s%n", header, t.ToUniversalString(),
                t.toString());
    }
}

1 个答案:

答案 0 :(得分:2)

您反复执行以下操作:

this.totalSeconds = (hour * 3600);
this.totalSeconds = (minute * 60);
this.totalSeconds = second;

这有效地覆盖了三次,因此只能观察到最终值。

您正在寻找的是

this.totalSeconds = (hour * 3600);
this.totalSeconds += (minute * 60);
this.totalSeconds += second;

否则你只是在每一行都覆盖它。

此外,您使用%来计算小时/分钟。例如,如果总秒数为3672(1小时,1分钟,12秒),则无效。

3672%3600 = 72.那不是几个小时。

几个小时,你需要totalSeconds / 3600;分钟(totalSeconds - (3600 * getHours())) / 60和秒totalSeconds - (3600 * getHours()) - (60 * getMinutes())

很抱歉编辑,已经很晚了,我无法数学。