第三章第三十题(当前时间)(Current time)

    科技2022-08-07  116

    第三章第三十题(当前时间)(Current time)

    *3.30(当前时间)修改编程练习题2.8,以12小时时钟制显示小时数。 下面是一个运行示例: Enter the time zone offset to GMT:-5 The current time is 4:50:34 AM

    *3.30(Current time) Revise Programming Exercise 2.8 to display the hour using a 12-hour clock. Here is a sample run: Enter the time zone offset to GMT:-5 The current time is 4:50:34 AM

    参考代码:

    package chapter03; import java.util.Scanner; public class Code_30 { public static void main(String[] args) { int ZoneOffset; long CurrHours,CurrMinutes,CurrSeconds; long CurrentMilliSeconds; CurrentMilliSeconds = System.currentTimeMillis(); CurrSeconds = CurrentMilliSeconds / 1000 % 60; CurrMinutes = CurrentMilliSeconds / 1000 / 60 % 60; CurrHours = CurrentMilliSeconds / 1000 / 60 / 60 % 24; System.out.print("Enter the time zone offset to GMT : "); Scanner ZoneOffsetInput = new Scanner(System.in); ZoneOffset = ZoneOffsetInput.nextInt(); CurrHours = (CurrHours + ZoneOffset + 24) % 24; if(CurrHours <= 12) System.out.println("The current time is " + CurrHours + ":" + CurrMinutes + ":" + CurrSeconds + " AM"); else { CurrHours %= 12; System.out.println("The current time is " + CurrHours + ":" + CurrMinutes + ":" + CurrSeconds + " PM"); } ZoneOffsetInput.close(); } } 结果显示: Enter the time zone offset to GMT : 8 The current time is 11:15:40 AM Process finished with exit code 0
    Processed: 0.016, SQL: 9