【问题标题】:How to find the duration between 2 Instant timestamp [closed]如何找到2个即时时间戳之间的持续时间[关闭]
【发布时间】:2017-01-29 08:24:08
【问题描述】:

我试图显示程序首次启动时的时间戳和停止的时间。 然后我需要找到开始和结束之间的持续时间。 以上信息都需要打印出来

但我无法区分代码中的两个即时时间(开始、结束)。 这就是我所拥有的,我知道我不应该使用 null 但你明白了。

public static void main(String[] args) {
    if (args.length == 0) {
        Instant t1 = null;
        Instant t2 = null;
        Duration duration = null;
        System.out.println(t1.now());
        System.out.println("Input data is missing. Expecting player data.");
        System.out.println(t2.now());
        System.out.println(duration.between(t1, t2));
        System.exit(-1);
    }

【问题讨论】:

  • 让我猜猜,你在System.out.println(t1.now());上有NullPointerException
  • 你真的应该开始用null以外的东西来初始化你的对象。

标签: java timestamp


【解决方案1】:

您可能会得到NullPointerException,因为now()Instant 中的一个静态变量,而您将它与Instant 的对象一起使用。也不需要声明Duration 类的对象,因为between()Duration 类来说是静态的。

public static void main(String[] args) {
if (args.length == 0) {
    Instant t1, t2;
    t1 = Instant.now();
    System.out.println(t1);
    System.out.println("Input data is missing. Expecting player data.");
    t2 = Instant.now();
    System.out.println(t2);
    System.out.println(Duration.between(t1, t2));
    System.exit(-1);
}

【讨论】:

  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2012-09-28
相关资源
最近更新 更多