第二章节 2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,然后显示在这个特定时区的时间。

文章提供了一个Java程序,该程序显示当前的格林尼治标准时间,并允许用户输入时区偏移量以显示相应时区的时间。通过Scanner类获取用户输入,然后调整小时以反映时区差异。程序适用于JDK1.8及更高版本。
摘要由CSDN通过智能技术生成

题目

题目描述

题目图片

2.8(当前时间)程序清单2-7给出了显示当前格林尼治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,然后显示在这个特定时区的时间。下面是一个运行示例:

Enter the time zone offset to GMT: -5 Enter
The current time is 4:50:34

解析

本题要求要有输入和输出。对于输入,我们需要先构造Scanner类对象,且要和标准输入流System.in关联,实现数据的输入,注意读取的数据类型应该采用哪种方法获取输入。

备注(2-7程序清单):

public class ShowCurrentTime {
    public static void main(String[] args) {
        // Obtain the total milliseconds since midnight, Jan 1, 1070
        long totalMilliseconds = System.currentTimeMillis();

        // Obtain the total seconds since midnight, Jan 1, 1070
        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 % 24;

        // Display results
        System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
    }
}

代码

本题jdk1.8和jdk18代码通用

jdk1.8和jdk18代码示例

代码展示

import java.util.Scanner;

public class Test2_8 {
    public static void main(String[] args) {
        // Obtain the total milliseconds since midnight, Jan 1, 1070
        long totalMilliseconds = System.currentTimeMillis();

        // Obtain the total seconds since midnight, Jan 1, 1070
        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 % 24;

        /** 修改 **/
        System.out.print("Enter the time zone offset to GMT:");
        Scanner input = new Scanner(System.in);
        int offset = input.nextInt(); // 读取偏移量
        currentHour = (currentHour + offset) % 24; // currentHour + offset如果小于0得到的结果会不正确,对24取余可以得到正确答案
        /** 修改 **/

        // Display results
        System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
    }
}

运行结果(以+8时区为例)

Enter the time zone offset to GMT:+8
Current time is 12:32:54 GMT
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值