exercise6_7.java

来自「Introduction to java programming 一书中所有编程」· Java 代码 · 共 50 行

JAVA
50
字号
public class Exercise6_7 {
  public static void main(String[] args) {
    Time time = new Time();
    System.out.println("Hour: " + time.getHour() + " Minute: " +
      time.getMinute() + " Second: " + time.getSecond());
  }
}

class Time {
  private int hour;
  private int minute;
  private int second;

  public Time() {
    // Obtain the total seconds since the midnight, Jan 1, 1970
    long totalSeconds = System.currentTimeMillis() / 1000;

    // Compute the current second in the minute in the hour
    second = (int) (totalSeconds % 60);

   // Obtain the total minutes
    long totalMinutes = totalSeconds / 60;

    // Compute the current minute in the hour
    minute = (int) (totalMinutes % 60);

    // Obtain the total hours
    long totalHours = totalMinutes / 60;

    // Compute the current hour
    hour = (int) (totalHours % 24);
  }

  public String toString() {
    return hour + ":" + minute + ":" + second + " GMT";
  }

  public int getHour() {
    return hour;
  }

  public int getMinute() {
    return minute;
  }

  public int getSecond() {
    return second;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?