Java黑皮书课后题第2章:*2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,显示在这个特定时区的时间
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第2章:*2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,显示在这个特定时区的时间
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
*2.8(當(dāng)前時(shí)間)程序清單2-7給出了顯示當(dāng)前格林尼治時(shí)間的程序。修改這個(gè)程序,提示用戶輸入相對(duì)于GMT的時(shí)區(qū)偏移量,顯示在這個(gè)特定時(shí)區(qū)的時(shí)間
- 題目
- 題目描述
- 運(yùn)行示例
- 程序清單2-7(非本題代碼)+思路(注釋)
- 代碼塊
題目
題目描述
*2.8(當(dāng)前時(shí)間)程序清單2-7給出了顯示當(dāng)前格林尼治時(shí)間的程序。修改這個(gè)程序,提示用戶輸入相對(duì)于GMT的時(shí)區(qū)偏移量,顯示在這個(gè)特定時(shí)區(qū)的時(shí)間
運(yùn)行示例
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可以在這里獲取時(shí)區(qū)// 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可以在這里將求出的秒數(shù)+時(shí)區(qū)和GMT時(shí)區(qū)的偏移(秒數(shù))// 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");} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第2章:*2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,显示在这个特定时区的时间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java黑皮书课后题第2章:2.6(求一
- 下一篇: Java黑皮书课后题第2章:2.9(物理