現在方法()Java 中的例項


可以使用 Java 中 Instant 類中的 now() 方法來獲得系統 UTC 時鐘中的當前時間點。此方法不需要引數,它會從系統 UTC 時鐘中返回當前時間點。

演示此方法的程式如下 −

示例

 線上演示

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      Instant i = Instant.now();
      System.out.println("The current Instant is: " + i);
   }
}

輸出

The current Instant is: 2019-02-12T11:49:22.455Z

現在讓我們理解上述程式。

我們使用 now() 方法從系統 UTC 時鐘中獲取當前時間點並打印出來。演示此方法的程式碼片段如下 −

Instant i = Instant.now();
System.out.println("The current Instant is: " + i);

更新於: 30-Jul-2019

8K+ 瀏覽

開始你的 事業

完成課程後獲取認證

開始
廣告
© . All rights reserved.