输入相对于GMT的时区偏移量,求这个特定时区的时间,修改下面程序实现

小巫技术博客
腾讯高级客户端工程师
博客专家认证
2012-06-18 10:49:55
public class ShowCurrentTime {

public static void main(String[] args) {
//获得从1970年1月1日0点到当前时间之间单位为毫秒的差值
long totalMilliseconds = System.currentTimeMillis();

//获取总的秒数
long totalSeconds = totalMilliseconds / 1000;

//计算当前秒数
long currentSecond = totalSeconds % 60;

//获取总分钟数
long totalMinutes = totalSeconds / 60;

//计算当前分钟数
long currentMinute = totalMinutes % 60;

//获取总的小时数
long totalHours = totalMinutes / 60;

//计算当前小时数
long currentHour = totalHours % 60;

//输出结果
System.out.println("Current time is " + currentHour + ":"
+ currentMinute + ":" + currentSecond + "GMT");
}

}
...全文
847 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小林有点忙 2013-02-07
  • 打赏
  • 举报
回复
楼主给的程序是改进之前的 修改之后的(关于时区偏移量): import java.util.Scanner; public class Exercise2_25 { public static void main(String[] args) { // Prompt the user to enter the time zone offset to GMT Scanner input = new Scanner(System.in); System.out.print("Enter the time zone offset to GMT: "); long timeZoneOffset = input.nextInt(); // Obtain the total milliseconds since the midnight, Jan 1, 1970 long totalMilliseconds = System.currentTimeMillis(); // Obtain the total seconds since the midnight, Jan 1, 1970 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 + timeZoneOffset) % 24; // Display results System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond); }
  • 打赏
  • 举报
回复
没看明白!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧