java clock_Java Clock tick()用法及代码示例

本文详细介绍了Java时间API中的Clock.tick()方法,该方法用于创建一个基于给定基础时钟并按照指定持续时间进行四舍五入的时钟。示例展示了如何使用此方法创建每10秒、30秒和3秒滴答的时钟,并解释了不同持续时间下的时钟表现。此外,还展示了如何处理以分钟、小时和天为单位的持续时间。
摘要由CSDN通过智能技术生成

java.time.Clock的tick(Clock baseClock,Duration tickDuration)方法是Clock类的静态方法,该方法返回一个时钟,该时钟返回从基本时钟开始的四舍五入到参数中指定持续时间的最接近值的时刻。指定的基本时钟持续时间必须为正,不能为负,也不能为null。时钟将按照给定的持续时间滴答,如果持续时间是一分钟的一半或一秒的一半或一小时的一半,时钟将返回四舍五入到一分钟的一半或一秒或一半的一半的时间。小时,由持续时间指定。

此方法有助于自定义根据持续时间进行滴答的时钟类别。例如,滴答表示如果持续时间为5秒,则瞬间的第二部分将根据5秒取整。如果基本时钟的第二部分是13,则它将是10。四舍五入的时钟返回小于基本时钟的瞬间。

零或一纳秒的持续时间将不会对基本时钟方法产生影响。这些将返回相同的基本时钟。如果基本时钟是不可变的,线程安全的和可序列化的,则返回的时钟也将是不可变的,线程安全的和可序列化的。

用法:

public static Clock tick(Clock baseClock, Duration tickDuration)

参数:此方法采用两个强制性参数:

baseClock-您要根据持续时间四舍五入的基本时钟。

tickDuration-每个可见的滴答声的持续时间,不舍弃,不为null。

返回值:此方法返回使用默认区域中最佳可用系统时钟的时钟

异常:此方法引发以下异常:

IllegalArgumentException-如果持续时间为负数,或者部分很小

而不是一整毫秒,这样整个持续时间就不能被整分为一秒。

ArithmeticException-如果持续时间太大而无法以纳秒表示。

例:

Code:

Clock baseClock = Clock.systemDefaultZone();

Clock clock = Clock.tick(baseClock, Duration.ofSeconds(10));

Expalnation::

method tick() returns the instant which ticks after per 10 sec means the duration

of the tick is 10sec.is instant actual time is 18:57:51.248Z then due to 10sec duration

it will round to 18:57:50Z and same for 18:59:36.247Z to 18:59:30Z.

下面的程序演示了java.time.Clock类的tick()方法

程序1:用systemDefaultZone()创建时钟时。

在下面的程序中,存在三种情况,持续时间为30秒,10秒,3秒。因此对于这三种情况必须使用tick()方法。

// Java program to demonstrate

// tick() method of Clock class

import java.time.*;

// create class

public class tickMethodDemo {

// Main method

public static void main(String[] args)

{

// create base Clock with systemDefaultZone() method

Clock baseclock = Clock.systemDefaultZone();

// get instant of base class and print instant

Instant instant = baseclock.instant();

System.out.println("Instant of Base class " + instant);

// apply tick Method for Duration of 30sec and

// create clock1 and print instant of clock1

Clock clock1 = Clock.tick(baseclock, Duration.ofSeconds(30));

System.out.println("Instant of Clock1 " + clock1.instant());

// apply tick Method for Duration of 10sec and

// create clock2 and print instant of clock2

Clock clock2 = Clock.tick(baseclock, Duration.ofSeconds(10));

System.out.println("Instant of Clock2 " + clock2.instant());

// apply tick Method for Duration of 3sec and

// create clock3 and print instant of clock3

Clock clock3 = Clock.tick(baseclock, Duration.ofSeconds(3));

System.out.println("Instant of Clock3 " + clock3.instant());

}

}

输出:

Instant of Base class 2018-08-22T11:18:11.336Z

Instant of Clock1 2018-08-22T11:18:00Z

Instant of Clock2 2018-08-22T11:18:10Z

Instant of Clock3 2018-08-22T11:18:09Z

程序2:当持续时间以分钟,小时或天为单位时,打印时钟时刻。

// Java program to demonstrate

// tick() method of Clock class

import java.time.*;

// create class

public class tickMethodDemo {

// Main method

public static void main(String[] args)

{

// create base Clock with systemUTC() method

Clock baseclock = Clock.systemUTC();

// get instant of base class and print instant

Instant instant = baseclock.instant();

System.out.println("Instant of Base class "

+ instant);

// apply tick Method for Duration of 10 Minutes

Clock clock1 = Clock.tick(baseclock,

Duration.ofMinutes(10));

System.out.println("Instant of Clock1 when duration"

+ " = 10 minutes is "

+ clock1.instant());

// apply tick Method for Duration of 2 hours

Clock clock2 = Clock.tick(baseclock,

Duration.ofHours(2));

System.out.println("Instant of Clock2 when duration"

+ " = 2 hours is "

+ clock2.instant());

// apply tick Method for Duration of 5 days

Clock clock3 = Clock.tick(baseclock,

Duration.ofDays(5));

System.out.println("Instant of Clock2 when duration"

+ " = 5 days is "

+ clock3.instant());

}

}

输出:

Instant of Base class 2018-08-22T11:18:15.533Z

Instant of Clock1 when duration = 10 minutes is 2018-08-22T11:10:00Z

Instant of Clock2 when duration = 2 hours is 2018-08-22T10:00:00Z

Instant of Clock2 when duration = 5 days is 2018-08-22T00:00:00Z

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值