自午夜以来的总秒数,Java

时间:2016-04-19 07:00:39

标签: java

我真的很喜欢编程。我正在为我的Java简介做一个任务。在我的任务中,我们需要找到自午夜以来的总秒数,并将此数字更改为小时,分钟和秒以显示当前时间。我有小问题。当我测试我的代码时,totalseconds显示0!任何帮助,将不胜感激。对不起,代码是一个混乱

        package clock;
import java.text.DecimalFormat;
import java.util.Calendar;  // to get current time

public class Clock {
    public static int totalseconds;
    public static int seconds;
    public static int minutes;
    public static int hours;

    public static int test;

    public Clock(int hours, int minutes, int seconds )  {
        setHours(hours);
        setMinutes(minutes);
        setSeconds(seconds);
    }


    // use current time
    public Clock() {
        Calendar c = Calendar.getInstance();
        long now = c.getTimeInMillis();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        long passed = now - c.getTimeInMillis();
        long secondsPassed = passed / 1000;
        totalseconds =  (int) secondsPassed;


    }


    public void tick() {
        addSecond();
    }

    private void addSecond() {
        seconds = totalseconds%60;
    }

    private void addMinute() {
        minutes = totalseconds/60 % 60;
    }

    private void addHour() {
        hours = totalseconds / 3600; 
    }

    public String toString() {
        DecimalFormat f = new DecimalFormat("00");
        return f.format(hours) + ":" + f.format(minutes) + ":" + f.format(seconds);
    }

    public int getHours() {
        return hours;
    }
    public int getMinutes() {
        return minutes;
    }
    public int getSeconds() {
        return seconds;
    }

    // the total number of minutes sinces midnight
    public int getTotalMinutes() {
        return totalseconds / 60 % 60;
    }

    // the total number of seconds since midnight 
    public int getTotalSeconds() {
        return totalseconds;
    }

    public void setHours(int hours) {
        if (hours > 23 || hours < 0) {
            this.hours = 0;
        }
        else
            this.hours = hours;
    }
    public void setMinutes(int minutes) {
        if (minutes > 59 || minutes < 0)
            this.minutes = 0;
        else
            this.minutes = minutes;
    }
    public void setSeconds(int seconds) {
        if (seconds > 59 || seconds < 0)
            this.seconds = 0;
        else
            this.seconds = seconds;
    }

    // reset hours, minutes and seconds to zero
    public void reset() {
        hours = minutes = seconds = 0;
    }
    public static void main(String [] args){

        System.out.println("this is total seconds " + test + totalseconds );
    }


}

2 个答案:

答案 0 :(得分:1)

将主要内容更改为此。

public static void main(String [] args){
    Clock clock = new Clock();
    System.out.println("this is total seconds " + test + totalseconds );
}

新建一个Clock实例,调用构造函数,你的所有魔法都会发生。

答案 1 :(得分:0)

我同意@shmosel建议的内容。在如何使用实例变量方面似乎存在混淆。此外,我认为您应该利用Java 8方法来完成您的时间部分。

class Clock {
    private int totalseconds;
    private int seconds;
    private int minutes;
    private int hours;

    int test;

    public Clock(int hours, int minutes, int seconds) {
      setHours(hours);
      setMinutes(minutes);
      setSeconds(seconds);
    }

    // use current time
public Clock() {
    LocalTime now = LocalTime.now(ZoneId.systemDefault());
    totalseconds = now.toSecondOfDay();
}

public void tick() {
    addSecond();
}

private void addSecond() {
    seconds = totalseconds % 60;
}

private void addMinute() {
    minutes = totalseconds / 60 % 60;
}

private void addHour() {
    hours = totalseconds / 3600;
}

public String toString() {
    DecimalFormat f = new DecimalFormat("00");
    return f.format(hours) + ":" + f.format(minutes) + ":" + f.format(seconds);
}

public int getHours() {
    return hours;
}

public int getMinutes() {
    return minutes;
}

public int getSeconds() {
    return seconds;
}

// the total number of minutes since midnight
public int getTotalMinutes() {
    return totalseconds / 60 % 60;
}

// the total number of seconds since midnight
public int getTotalSeconds() {
    return totalseconds;
}

public void setHours(int hours) {
    if (hours > 23 || hours < 0) {
        this.hours = 0;
    } else
        this.hours = hours;
}

public void setMinutes(int minutes) {
    if (minutes > 59 || minutes < 0)
        this.minutes = 0;
    else
        this.minutes = minutes;
}

public void setSeconds(int seconds) {
    if (seconds > 59 || seconds < 0)
        this.seconds = 0;
    else
        this.seconds = seconds;
}

// reset hours, minutes and seconds to zero
public void reset() {
    hours = minutes = seconds = 0;
}
}

public class SecondsSinceMidnight {

  public static void main(String[] args) {
    Clock myClock = new Clock();
    System.out.println("Total seconds that elapsed since midnight:" + myClock.getTotalSeconds());
    System.out.println("Converted to Minutes: " + myClock.getTotalMinutes());
  }

}