java now
Instant Class now()方法 (Instant Class now() method)
Syntax:
句法:
public static Instant now();
public static Instant now(Clock cl);
now() method is available in java.time package.
now()方法在java.time包中可用。
now() method is used to get the current instant generated from the system clock.
now()方法用于获取从系统时钟生成的当前时刻。
now(Clock cl) method is used to return the current instant generated from the given Clock.
now(Clock cl)方法用于返回从给定Clock生成的当前时刻。
These methods don't throw an exception at the time of calculating the remainder.
这些方法在计算余数时不会引发异常。
These are static methods and it is accessible with the class name and if we try to access these methods with the class object then we will not get an error.
这些是静态方法,可通过类名进行访问,如果尝试使用类对象访问这些方法,则不会出错。
Parameter(s):
参数:
In the first case, "now()",
在第一种情况下,“ now()”
- None
In the second case, "now(Clock cl)",
在第二种情况下,“ now(Clock cl)”
- Clock cl – represents the clock to use to represent this Instant.
- Clock cl –代表用来表示此Instant的时钟。
Return value:
返回值:
In both the cases, the return type of the method is Instant,
在这两种情况下,方法的返回类型均为Instant 。
In the first cases, it returns the Instant obtained from the system clock.
在第一种情况下,它返回从系统时钟获得的Instant。
In the second cases, it returns the Instant obtained from the given clock.
在第二种情况下,它将返回从给定时钟获得的Instant。
Example:
例:
// 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);
}
}
Output
输出量
Instant.now(): 2020-05-28T01:17:43.547677Z
Instant.now(cl): 2020-05-28T01:17:43.618370Z
翻译自: https://www.includehelp.com/java/instant-now-method-with-example.aspx
java now