*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的时区偏移量,显示在这个特定时区的时间相关推荐

  1. Java黑皮书课后题第9章:*9.4(使用Random类)编写一个程序,创建一个种子为1000的Random对象,然后使用nextInt(100)方法显示0到100之间的前50个随机整数

    Java黑皮书课后题第9章:*9.4(使用Random类)编写一个程序,创建一个种子为1000的Random对象,然后使用nextInt方法显示0到100之间的前50个随机整数 题目 赘述 代码 题目 ...

  2. Java黑皮书课后题第9章:*9.3(使用Date类)编写程序创建一个Date对象,设置它的流逝时间分别为...,然后使用toString()方法分别显示上述日期

    Java黑皮书课后题第9章:*9.3(使用Date类)编写程序创建一个Date对象,设置它的流逝时间分别为...,然后使用toString方法分别显示上述日期 题目 代码 思路 代码 题目 代码 思路 ...

  3. Java黑皮书课后题第1章:1.12(以千米计的平均速度)假设一个跑步者1小时40分35秒跑了24英里。编写一个程序显示以每小时为多少千米为单位的平均速度值(1英里等于1.6千米)

    Java黑皮书课后题第1章:1.12(以千米计的平均速度) 题目 题目描述 破题 代码块 修改日志 题目 题目描述 1.12(以千米计的平均速度)假设一个跑步者1小时40分35秒跑了24英里.编写一个 ...

  4. Java黑皮书课后题第1章:*1.11(人口估算)编写一个程序,显示未来5年的每年人口数。假设当前的人口是312 032 486,每年有365天

    Java黑皮书课后题第1章:*1.11(人口估算) 题目 题目描述 破题 代码块 方法评析 为什么print函数内的表达式不能分开 修改日志 题目 题目描述 *1.11(人口估算)编写一个程序,显示未 ...

  5. Java黑皮书课后题第1章:1.7(求π的近似值)编写程序,显示4*(1-1/3+1/5-1/7+1/9-1/11【+1/13】)

    Java黑皮书课后题第1章:1.7(求π的近似值) 题目描述 代码 代码块 评析 修改日志 题目描述 可以使用以下公式计算π: [手动空格]π=4*(1-1/3+1/5-1/7+1/9-1/11+-) ...

  6. Java黑皮书课后题第1章:1.6(数列求和)编写程序,显示1+2+3+4+5+6+7+8+9的结果

    Java黑皮书课后题第1章:1.6(数列求和) 题目 题目描述 槽点 代码 代码块 区分println(x)与println("x") 法1法2选用 修改日志 题目 题目描述 1. ...

  7. Java黑皮书课后题第1章:1.5(计算表达式)编写程序,显示以下式子的结果

    Java黑皮书课后题第1章:1.5(计算表达式) 题目 题目描述 题目槽点 代码 代码块 代码评析与易错点 方法选用 易错点 非常不舒服的运算符前后空格(对新手来讲) 修改日志 题目 题目描述 编写程 ...

  8. Java黑皮书课后题第1章:1.3(显示图案)编写程序,显示下面的图案 Java

    Java黑皮书课后题第1章:1.3编写程序,显示下面的图案 题目 题目描述 题目槽点 代码片段 赘述 修改日志 题目 题目描述 编写程序,显示(自己语言描述,详见下图:由不同数量的小字母组成JAVA大 ...

  9. Java黑皮书课后题第1章:1.13(代数:求解2*2线性方程组)编写程序,求解以下方程组并显示x和y的值 3.4x+50.2y=44.5 2.1x+0.55y=5.9

    Java黑皮书课后题第1章:(代数:求解2*2线性方程组) 题目 题目描述 破题 代码块 方法评析 赘述 修改日志 题目 题目描述 求x和y值: 3.4x+50.2y=44.5 2.1x+0.55y= ...

最新文章

  1. Observer设计模式【利用商品概念解释】
  2. MyBatis 的这些坑你有踩过吗?
  3. Java内存使用量测试 看看我们天天在用的JVM到底浪费了多少内存资源
  4. 理论基础 —— 查找 —— 二叉排序树
  5. Android和iOS智能机去年出货超7亿 同比增长46%
  6. PHP学习记录(数学函数库)
  7. 在多行中查找和替换vim中的字符串
  8. IDEA整合Spring Boot项目访问jsp文件
  9. Android 7.1 App Shortcuts使用
  10. 安装使用dubbo-admin管理台进行服务监控和服务治理
  11. Android的ALSA声卡
  12. BaseAdapter使用的三种形式,逗比式,普通式,文艺式
  13. 用C#语言实现http协议下的多线程文件传输
  14. 金蝶K3与百望九赋税控开票软件对接实现半自动开票
  15. elasticsearch 支持拼音检索
  16. 从零开始学GIMP:一.从基本图形开始
  17. Tourists——圆方树
  18. 网站入侵工具之wscan使用详解
  19. Android——集地图、定位、导航于一体
  20. autojs教程:找图函数

热门文章

  1. java编译器代码检查_java 命名代码检查-注解处理器
  2. 工作和人工智能的未来
  3. ​5G行业应用成熟度洞察,哪些场景将率先起飞?|新基建技术洞察之
  4. 调度算法为何被阿里如此重视?
  5. GTS来了!阿里微服务架构下的分布式事务解决方案
  6. HTML提交弹出提交中,javascript – 从弹出窗口在父窗口中提交表单?
  7. 网游类似魔兽世界的服务器维护都是在干嘛?
  8. 文档扫描OCR识别-1(python)
  9. docker启动.NET3.1与5.0的包
  10. Mongodb在renameCollection之后,记得千万要立即补建索引