저는 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);
}
}