1

私はJavaが初めてで、このプログラムを練習として書こうとしています。プログラムは、現在のタイム ゾーン オフセットを取得し、現在の時刻を表示します。しかし、私の時間がマイナスになっている方法もあります。ここに論理エラーがあると思いますが、見つかりません。

Enter the time zone offset to GMT: -4
The current time: -2:48:26

私はニューヨークを使用しています (GMT -4 時間)

// A program that display the current time, with the user input a offset

import java.util.Scanner;

class CurrentTime {
    public static void main(String[] args) {
        // Create a Scanner object
        Scanner input = new Scanner(System.in);
        long totalMillSeconds = System.currentTimeMillis();

        long totalSeconds = totalMillSeconds / 1000;
        long currentSecond = (int)totalSeconds % 60;

        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;

        long totalHours = totalMinutes / 60;
        long currentHour = totalHours % 24;

        // Prompt user to ask what is the time zone offset
        System.out.print("Enter the time zone offset to GMT: ");
        long offset = input.nextLong();

        // Adjust the offset to the current hour
        currentHour = currentHour + offset;
        System.out.print("The current time: " + currentHour + ":" 
                + currentMinute + ":" + currentSecond);

    }
}
4

2 に答える 2