我是新來的Java後-4,我試圖寫這個程序的做法。該程序採用當前時區偏移量並顯示當前時間。但有些時候我的時間會消極。我認爲這裏有一個邏輯錯誤,但我找不到它。的CURREN時間是負偏移
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);
}
}