(当前时间)程序清单2-7 给出了显示当前格林威治时间的程序。修改这个程序,提示用户输入相 对于 GMT的时区偏移量,然后显示在这个特定时区的时间。下面是一个运行示例:
import java.util.Scanner;
public class Test2_8 {
public static void main(String[] args) {
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds /1000;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes /60;
long currentHour = totalHours % 24;
System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
Scanner input = new Scanner(System.in);
System.out.println("Enter the time zone offset to GMT: ");
int offset = input.nextInt();
long offsetGMT =(currentHour + offset) + currentMinute + currentSecond;
System.out.println("The current time is " + (currentHour + offset) + ":" + currentMinute + ":" + currentSecond + " GMT");
}
}