【问题标题】:how can i display current time in java using currentTimeMillis() method如何使用 currentTimeMillis() 方法在 java 中显示当前时间
【发布时间】: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

标签: java methods


【解决方案1】:

我认为这个帖子已经回答了这个问题:How to transform currentTimeMillis to a readable date format? [duplicate]

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多