java now_Java即时类

Instant Class now()方法 句法

public static Instant now();
    public static Instant now(Clock cl);

  

  • now()方法在java.time包中可用。
  • now()方法用于获取从系统时钟生成的当前时刻。
  • now(Clock cl)方法用于返回从给定Clock生成的当前时刻。
  • 这些方法在计算余数时不会引发异常。
  • 这些是静态方法,可通过类名进行访问,如果尝试使用类对象访问这些方法,则不会出错。 


参数:

  • 在第一种情况下,“ now()”       None
  • 在第二种情况下,“ now(Clock cl)”      Clock cl –代表用来表示此Instant的时钟。

返回值:在这两种情况下,方法的返回类型均为Instant 。

  •     在第一种情况下,它返回从系统时钟获得的Instant。
  •     在第二种情况下,它将返回从给定时钟获得的Instant。

例:

// Java program to demonstrate the example 
// of now() method of Instant
import java.time.*;
import java.time.temporal.*;

public class NowOfInstant {
    public static void main(String args[]) {
        // Instantiates a ZoneId for Accra
        // and a Clock object
        ZoneId zone = ZoneId.of("Africa/Accra");
        Clock cl = Clock.system(zone);

        // Here, this method returns the
        // current instant from the default 
        // system clock
        Instant ins = Instant.now();

        // Display ins
        System.out.println("Instant.now(): " + ins);

        // Here, this method returns the
        // current instant from the given 
        // clock (cl)
        ins = Instant.now(cl);

        // Display ins
        System.out.println("Instant.now(cl): " + ins);
    }
}

输出量

Instant.now(): 2020-05-28T01:17:43.547677Z
Instant.now(cl): 2020-05-28T01:17:43.618370Z

 

posted @ 2020-12-28 20:50  summer_xbc  阅读(336)  评论(0)    收藏  举报