📜  Java中的时钟滴答()方法与示例

📅  最后修改于: 2022-05-13 01:55:02.716000             🧑  作者: Mango

Java中的时钟滴答()方法与示例

Java.time.Clocktick(Clock baseClock, Duration tickDuration)方法是Clock 类的静态方法,它返回一个时钟,该时钟返回从基本时钟四舍五入到参数中指定持续时间的最近出现处的瞬间。指定的基本时钟持续时间必须是正数,既不是负数也不是空值。时钟将按照给定的持续时间滴答作响,如果持续时间是半分钟或半秒或半小时,时钟将返回四舍五入到半分钟或半秒或半小时的瞬间小时,由持续时间指定。

此方法可以帮助自定义根据持续时间滴答的时钟类。例如,勾号表示如果提供 5 秒的持续时间,则瞬间的第二部分将根据 5 秒四舍五入。如果基准时钟秒部分是 13,那么它将是 10。舍入时钟返回小于基准时钟的瞬间。

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

句法:

public static Clock tick(Clock baseClock, Duration tickDuration)

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

  • baseClock - 您要根据持续时间四舍五入的基本时钟。
  • tickDuration – 每个可见刻度的持续时间以使时钟四舍五入,不为负,不为空。

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

异常:此方法抛出以下异常:

  • IllegalArgumentException – 如果持续时间为负数,或者有一部分非常小
    比整毫秒,这样整个持续时间不能被整除为一秒。
  • ArithmeticException – 如果持续时间太大而无法以纳秒表示。

例子:

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

Explanation::
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 the 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 the 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 Java