当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Clock tick()用法及代码示例


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

参考:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#tick-java.time.Clock-java.time.Duration-



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Clock tick() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。