【发布时间】:2017-03-11 14:12:54
【问题描述】:
我想创建一个 java 程序,使用System.currentTimeMillis() 方法在 GMT 时区显示当前时间。
public class DisplayTime {
public static void main(String[] args){
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 = totalMinutes % 12;
System.out.println(currentHour + " : " + currentMinute + " : " + currentSecond);
}
}
【问题讨论】:
-
你的代码在哪里?所以。如果你表现出一些努力,可以提供帮助!
-
最简单的是
Instant.ofEpochMilli()。它的toString()将为您提供可接受的格式,或者您可以根据自己的喜好对其进行格式化。不知道算不算作弊。 -
您拥有的代码是否如您所愿?以什么方式不?
-
看起来它应该可以工作,除了
long currentHour = totalMinutes % 12需要使用totalHours而不是totalMinutes