Java黑皮书课后题第2章:*2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,显示在这个特定时区的时间
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第2章:*2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,显示在这个特定时区的时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*2.8(當前時間)程序清單2-7給出了顯示當前格林尼治時間的程序。修改這個程序,提示用戶輸入相對于GMT的時區偏移量,顯示在這個特定時區的時間
- 題目
- 題目描述
- 運行示例
- 程序清單2-7(非本題代碼)+思路(注釋)
- 代碼塊
題目
題目描述
*2.8(當前時間)程序清單2-7給出了顯示當前格林尼治時間的程序。修改這個程序,提示用戶輸入相對于GMT的時區偏移量,顯示在這個特定時區的時間
運行示例
Enter the time zone offset to GMT: -5
The current time is 4:50:34
程序清單2-7(非本題代碼)+思路(注釋)
public class ShowCurrentTime {public static void main(String[] args) {// 2.8可以在這里獲取時區// Obtain the total milliseconds since midnight, Jan 1, 1970long totalMilliseconds = System.currentTimeMillis();// Obtain the total seconds since midnight, Jan 1, 1970long totalSeconds = totalMilliseconds / 1000;//2.8可以在這里將求出的秒數+時區和GMT時區的偏移(秒數)// Compute the current second in the minute in the hourlong currentSecond = totalSeconds % 60;// Obtain the total minuteslong totalMinutes = totalSeconds / 60;// Compute the current minute in the hourlong currentMinute = totalMinutes % 60;// Obtain the total hourslong totalHours = totalMinutes / 60;// Compute the current hourlong currentHour = totalHours % 24;// Display resultsSystem.out.println("Current time is " + currentHour + ":"+ currentMinute + ":" + currentSecond + " GMT");} }代碼塊
import java.util.Scanner;public class Test2_8 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter the time zone offset to GMT:");int offset = input.nextInt();// Obtain the total milliseconds since midnight, Jan 1, 1970long totalMilliseconds = System.currentTimeMillis();// Obtain the total seconds since midnight, Jan 1, 1970long totalSeconds = totalMilliseconds / 1000;totalSeconds += offset * 60 * 60;// Compute the current second in the minute in the hourlong currentSecond = totalSeconds % 60;// Obtain the total minuteslong totalMinutes = totalSeconds / 60;// Compute the current minute in the hourlong currentMinute = totalMinutes % 60;// Obtain the total hourslong totalHours = totalMinutes / 60;// Compute the current hourlong currentHour = totalHours % 24;// Display resultsSystem.out.println("Current time is " + currentHour + ":"+ currentMinute + ":" + currentSecond + " GMT");} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第2章:*2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,显示在这个特定时区的时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第2章:2.6(求一
- 下一篇: Java黑皮书课后题第2章:2.9(物理