public class ShowCurrentTime { public static void main(String[] args) {
//获得从1970年1月1日0点到当前时间之间单位为毫秒的差值
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 % 60;

//输出结果
System.out.println("Current time is " + currentHour + ":"
+ currentMinute + ":" + currentSecond + "GMT");
}}

解决方案 »

  1.   

    楼主给的程序是改进之前的
    修改之后的(关于时区偏移量):
    import java.util.Scanner;public class Exercise2_25 {
      public static void main(String[] args) {
        // Prompt the user to enter the time zone offset to GMT
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the time zone offset to GMT: ");
        long timeZoneOffset = input.nextInt();
        
        // Obtain the total milliseconds since the midnight, Jan 1, 1970
        long totalMilliseconds = System.currentTimeMillis();    // Obtain the total seconds since the midnight, Jan 1, 1970
        long totalSeconds = totalMilliseconds / 1000;    // Compute the current second in the minute in the hour
        long currentSecond = totalSeconds % 60;    // Obtain the total minutes
        long totalMinutes = totalSeconds / 60;    // Compute the current minute in the hour
        long currentMinute = totalMinutes % 60;    // Obtain the total hours
        long totalHours = totalMinutes / 60;    // Compute the current hour
        long currentHour = (totalHours + timeZoneOffset) % 24;    // Display results
        System.out.println("Current time is " + currentHour + ":"
          + currentMinute + ":" + currentSecond);
      }