每日一课 | 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

翻译自: https://www.includehelp.com/java/instant-now-method-with-example.aspx

今日福利????

推荐阅读--

当一个程序员飞速敲键盘时,他在干嘛?

卧槽!最新编程语言排名,Java 沦为老二。。

为何程序员上班时间总戴个耳机,看完恍然大悟...

每日一课 | 如何将Java对象读取和写入文件

每日一课 | Java Final关键字示例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值