(当前时间)程序清单 2-7 给出了显示当前格林威治时间的程序。修改这个程序,提示用户输入相对于 GMT 的时区偏移量,然后显示在这个特定时区的时间。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dome2_8;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Dome2_8 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
long nowhours;
System.out.print("Enter the time zone offset to GMT:");
long deviation = in.nextInt();
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;
nowhours = currentHour + deviation;
if (nowhours < 0) {
nowhours = nowhours + 24;
}else if (nowhours >= 24) {
nowhours %= 24;
}
System.out.println("Current time is " + nowhours + ":" + currentMinute + ":" + currentSecond + " GMT");
}
}