Java Time2从3个整数变为单个整数

Java Time2从3个整数变为单个整数,java,Java,我试图将time2类从3个整数改为一个整数,以节省空间。我已经去掉了小时、分钟和秒,现在只有totalseconds来表示从午夜开始的时间。我在set和get方法以及3参数构造函数中替换了它,它在Time2Test应用程序中运行,但只读取秒数。所有这三个参数都被解读为秒,所以我不确定发生了什么。ex-Time2测试时间应为12:25:42,但应改为42:42:42。首先是Time2类,然后为了清晰起见,我还添加了Time2Test。任何指示都将不胜感激 public class Time2 {

我试图将time2类从3个整数改为一个整数,以节省空间。我已经去掉了小时、分钟和秒,现在只有totalseconds来表示从午夜开始的时间。我在set和get方法以及3参数构造函数中替换了它,它在Time2Test应用程序中运行,但只读取秒数。所有这三个参数都被解读为秒,所以我不确定发生了什么。ex-Time2测试时间应为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());
    }
}
公共类时间2{
私人整数秒;
//无参数构造函数
公共时间2(
{
this(0,0,0);//使用三个参数调用构造函数,默认值为0
}
//构造函数,提供的小时默认为分钟,秒默认为0
公共时间2(整小时)
{
this(hour,0,0);//使用3个参数调用构造函数
}
//提供小时和分钟的构造函数秒默认为0
公共时间2(整数小时,整数分钟)
{
this(小时、分钟、0);//使用3个参数调用构造函数
}
//Time2建造师还提供小时分钟和秒测试
公共时间2(整数小时、整数分钟、整数秒)
{       
这是1.totalseconds=(小时*3600);
这是1.totalseconds=(分钟*60);
this.totalseconds=(秒);
}
公共时间2(时间2)
{
//使用2个参数调用构造函数
这是(time.getHour(),time.getMinute(),time.getSecond());
}
//设置和获取方法从这里开始,还有通用时间转换和检查
公共无效设置时间(整数小时、整数分钟、整数秒)
{
如果(小时<0 | |小时>=24)
抛出新的IllegalArgumentException(“小时必须为0-23”);
如果(分钟<0 | |分钟>=59)
抛出新的IllegalArgumentException(“分钟必须为0-59”);
如果(秒<0 | |秒>=59)
抛出新的IllegalArgumentException(“小时必须为0-59”);
这是1.totalseconds=(小时*3600);
这是1.totalseconds=(分钟*60);
this.totalseconds=(秒);
}
//验证并设置时间
公共无效设置小时(整小时)
{
如果(小时<0 | |小时>=24)
抛出新的IllegalArgumentException(“小时必须为0-23”);
这是1.totalseconds=(小时*3600);
}
//验证并设置分钟
公共无效设置分钟(整数分钟)
{
如果(分钟<0 | |分钟>=59)
抛出新的IllegalArgumentException(“分钟必须为0-59”);
这是1.totalseconds=(分钟*60);
}
//验证并设置秒
公共无效设置秒(整数秒)
{
如果(秒<0 | |秒>=24)
抛出新的IllegalArgumentException(“秒数必须为0-59”);
this.totalseconds=(秒);
}
//获取方法从这里开始
//一小时
公共整数getHour()
{
返回总秒数%3600;
}
//分秒必争
公共int getMinute()
{
返回总秒数%60;
}
//获得第二名
公共int getSecond()
{
返回总秒数;
}
//将字符串转换为通用格式(HH:MM:SS)
公共字符串ToUniversalString()
{
返回字符串格式(
%02d:%02d:%02d”、getHour()、getMinute()、getSecond();
}
//转换为标准格式(H:MM:SS AM或PM)
公共字符串toString()
{
返回字符串。格式(“%d:%02d:%02d%s)”,((getHour()==0 | | getHour()==
12) ?12:getHour()%12)、getMinute()、getSecond()、getHour()
<12“上午”:“下午”);
}
}//下课时间2
包装时间2;
公共课时间2测试
{
公共静态void main(字符串[]args)
{
Time2 t1=new Time2();//00:00:00
time2t2=新的Time2(2);//02:00:00
time2t3=新的Time2(21,34);//21:34:00
time2t4=新的Time2(12,25,42);//12:25:42
time2t5=新的Time2(t4);//12:25:42
System.out.println(“用:”)构造);
显示时间(“t1:所有默认参数”,t1);
显示时间(“t2:指定的小时;默认为分钟和秒”,t2);
显示时间(“t3:提供的小时和分钟为秒默认值”,t3);
显示时间(“t4:提供时分和秒”,t4);
显示时间(“t5:指定的时间2对象t4”,t5);
//尝试使用无效参数初始化t6
尝试
{
Time2 t6=新Time2(27,74,99);//所有无效值
}
捕获(IllegalArgumentException e)
{
System.out.printf(“%n初始化t6时出现异常:%s%n”,
e、 getMessage());
}
}
//以24小时和12小时格式显示Time2对象
私有静态void displayTime(字符串头,time2t)
{
System.out.printf(“%s%n%s%n%s%n”,标题,t.ToUniversalString(),
t、 toString());
}
}

您可以重复执行以下操作:

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
;分钟
(总秒数-(3600*getHours())/60
,秒
总秒数-(3600*getHours())-(60*getMinutes())


抱歉编辑,时间太晚了,我无法计算。

您重复执行以下操作:

this.totalSeconds = (hour * 3600);
this.totalSeconds = (minute * 60);
this.totalSeconds = second;
这实际上会覆盖自身三次,因此只观察最终值

你要找的是

this.totalSeconds = (hour * 3600);
this.totalSeconds += (minute * 60);
this.totalSeconds += second;
否则,您只是在每行上覆盖它


此外,还可以使用
%
计算h